简体   繁体   中英

How to deserialize a JSON array into a struct using serde?

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?

Assuming you have control over the JSON format I strongly recommend making the Shape type into an enum that can represent multiple shapes and using serde's derive macros to automatically implement Serialize and Deserialize for Shape . Example:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Point {
    x: f64,
    y: f64,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
enum Shape {
    Dot { position: Point },
    Line { start: Point, end: Point },
    Circle { center: Point, radius: f64 },
}

fn main() {
    let shapes = vec![
        Shape::Dot {
            position: Point { x: 3.0, y: 4.0 },
        },
        Shape::Line {
            start: Point { x: -2.0, y: 1.0 },
            end: Point { x: 5.0, y: -3.0 },
        },
        Shape::Circle {
            center: Point { x: 0.0, y: 0.0 },
            radius: 7.0,
        },
    ];

    let serialized = serde_json::to_string(&shapes).unwrap();
    println!("serialized = {}", serialized);

    let deserialized: Vec<Shape> = serde_json::from_str(&serialized).unwrap();
    println!("deserialized = {:?}", deserialized);
}

playground

If you absolutely cannot change the JSON format then serde cannot help you. Serializing a shape as a heterogeneous array of strings and floats is a very bizarre choice. You have to manually parse it yourself (or at least use some parser crate to help you) and then manually implement the Deserializer trait for it to turn it into a Shape .

How should I define the struct of Shape to deserialize the JSON data as above?

You wouldn't, because the serialisation scheme you want doesn't really make sense to rust, and AFAIK serde doesn't support it (not even if you use an enum of tuple variant, tag="type" is not supported for them).

If you really can't or don't want to use a simpler structure & serialisation scheme as described in the other answer, the only option I can see is to implement a custom (de)serialisation scheme.

Especially since the arity changes for each "type", otherwise https://crates.io/crates/serde_tuple would work (although you could always see if skip_serializing_if works with serde_tuple, that would let you suppress the "extra" fields).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM