I'm trying to deserialize following JSON snippets into a Vec of struct Shape:
use serde::{Deserialize, Serialize};
use serde_json::{Result, Value};
#[derive(Debug, Serialize, Deserialize)]
struct Shape { // this struct is not working, for display purpose only
shape_type: String,
d0: f64,
d1: f64,
d2: f64, //optional, like the case of "dot"
d3: f64, //optional, like the case of "circle"
}
let json = r#"
{[
["line", 1.0, 1.0, 2.0, 2.0],
["circle", 3.0, 3.0, 1.0],
["dot", 4.0, 4.0]
]}"#;
let data: Vec<Shape> = match serde_json::from_str(json)?;
Obviously, each type of Shape needs a String and different number of f64 to describe it. How should I define the struct of Shape to deserialize the JSON data as above?
let json = r#"{"shapes": [["line", 1.0, 1.0, 2.0, 2.0], ..."#;Unfortunately I cannot change the JSON format. I tried to change the type of d2 & d3 to Option<f64>, but no luck:"invalid length 4, expected struct Shape with 5 elements"at line of "circle".