-
Hi, I'm struggling to figure out how to use this crate to deserialize a vec of a remote defined type. I have found a similar discussion about this: #225 ; but still can't understand how to implement this for my use case. I apologize if this is considered a duplicate. #[derive(Serialize, Deserialize)]
#[serde(remote = "Vector3")]
struct Vector3Def {
x: f32,
y: f32,
z: f32,
}
#[serde_as]
#[derive(Serialize, Deserialize, Debug)]
struct Route {
#[serde_as(as = "Vec<Vector3Def>")]
positions: Vec<Vector3>,
}
fn main() -> Result<()> {
// Some JSON input data as a &str. Maybe this comes from the user.
let data = r#"
{
"positions": [
{ "x":0, "y":0, "z:":0 },
{ "x":0, "y":0, "z:":0 },
{ "x":0, "y":0, "z:":0 },
{ "x":0, "y":0, "z:":0 },
]
}"#;
// Parse the string of data into serde_json::Value.
let v: Route = serde_json::from_str(data)?;
println!("{:#?}", v);
Ok(())
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The user guide in the documentation contains instructions how to combine the remote derive feature with |
Beta Was this translation helpful? Give feedback.
The user guide in the documentation contains instructions how to combine the remote derive feature with
serde_as
:https://docs.rs/serde_with/2.0.0/serde_with/guide/serde_as/index.html#using-serde_as-with-serdes-remote-derives