如何使用 serde 将 JSON 数组反序列化为结构?
How to deserialize a JSON array into a struct using serde?
我正在尝试将以下 JSON 片段反序列化为 Vec
结构 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)?;
显然,每一种Shape
都需要一个String
和不同数量的f64
来描述。我应该如何定义 Shape
的结构来反序列化上面的 JSON 数据?
假设您可以控制 JSON 格式,我强烈建议将 Shape
类型转换为可以表示多种形状的 enum
并使用 serde 的派生宏自动实现 Serialize
和 Deserialize
对应 Shape
。示例:
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);
}
如果您绝对不能更改 JSON 格式,则 serde 无法帮助您。将形状序列化为异构的字符串和浮点数数组是一个非常奇怪的选择。你必须自己手动解析它(或者至少使用一些解析器箱来帮助你)然后 manually implement the Deserializer trait 让它变成 Shape
.
How should I define the struct of Shape to deserialize the JSON data as above?
你不会,因为你想要的序列化方案对 rust 没有真正的意义,而且 AFAIK serde 不支持它(即使你使用 enum
的元组变体,tag="type"
他们不支持)。
如果您真的不能或不想使用其他答案中描述的更简单的结构和序列化方案,我能看到的唯一选择是实施自定义(反)序列化方案。
特别是因为每种“类型”的元数都会发生变化,否则 https://crates.io/crates/serde_tuple would work (although you could always see if skip_serializing_if
与 serde_tuple 一起使用,这会让您抑制“额外”字段)。
我正在尝试将以下 JSON 片段反序列化为 Vec
结构 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)?;
显然,每一种Shape
都需要一个String
和不同数量的f64
来描述。我应该如何定义 Shape
的结构来反序列化上面的 JSON 数据?
假设您可以控制 JSON 格式,我强烈建议将 Shape
类型转换为可以表示多种形状的 enum
并使用 serde 的派生宏自动实现 Serialize
和 Deserialize
对应 Shape
。示例:
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);
}
如果您绝对不能更改 JSON 格式,则 serde 无法帮助您。将形状序列化为异构的字符串和浮点数数组是一个非常奇怪的选择。你必须自己手动解析它(或者至少使用一些解析器箱来帮助你)然后 manually implement the Deserializer trait 让它变成 Shape
.
How should I define the struct of Shape to deserialize the JSON data as above?
你不会,因为你想要的序列化方案对 rust 没有真正的意义,而且 AFAIK serde 不支持它(即使你使用 enum
的元组变体,tag="type"
他们不支持)。
如果您真的不能或不想使用其他答案中描述的更简单的结构和序列化方案,我能看到的唯一选择是实施自定义(反)序列化方案。
特别是因为每种“类型”的元数都会发生变化,否则 https://crates.io/crates/serde_tuple would work (although you could always see if skip_serializing_if
与 serde_tuple 一起使用,这会让您抑制“额外”字段)。