Bria
April 25, 2023, 2:57am
1
I have a &str
:
let serde_str = r#"{"Convert":"Tf", "value":[0,1]}"#;
And I have a type:
#[derive(Debug, Serialize, Deserialize)]
pub enum NewConvert {
#[serde(rename(deserialize = r#"Convert::Tf"#))]
Tf(usize, usize),
}
How can I deserialize the serde_str
to a instance of my type NewConvert
:
serde_json::from_str::<NewConvert>(&serde_str)
The serde field attributes #[serde(rename)]
doesn't work, what am I doing wrong?
what is that Convert::TF
thing? is it a deserializer function? in that case, you probably want #[serde(deserialize_with = "Convert::TF")]
, the #[serde(rename)]
attribute is to rename the fields, but in your code, "Convert::TF" is not a valid field name
1 Like
You need to use serde
's tag
and content
attributes to deserialize a JSON like that to an enum:
#[serde(tag = "Convert", content = "value")]
pub enum NewConvert {
Tf(usize, usize),
}
This is documented at Enum representations · Serde
1 Like
system
Closed
July 24, 2023, 3:28am
4
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.