我有一个JSON,看起来像这样:
{
"name": "customer",
"properties": [
{
"name": "id",
"type": "int",
"value": 32
},
{
"name": "name",
"type": "string",
"value": "John"
}
]
}
目前我正在将数据反序列化为以下结构集:
#[derive(Serialize, Deserialize, Debug)]
struct Customer {
name: String,
properties: Vec<Property>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "name", content = "value")]
enum Property {
#[serde(rename = "id")]
Id(i32),
#[serde(rename = "name")]
Name(String),
}
为了避免每次访问属性时都要匹配枚举,我希望将其反序列化为一个类似于以下结构的结构体:
struct Customer {
name: String,
properties: Properties,
}
struct Properties {
id: i32, // will be 32 as in the object containing the name "id".
name: String, // will be John as in the object containing the name "name".
}
这是 serde
库是否以某种方式允许的内容?如果是,您能否提供一个示例来实现这一点?
请注意,我不能干扰实际的json结构,因此我不对需要进行任何更改的解决方案感兴趣。