【发布时间】:2020-05-07 09:28:01
【问题描述】:
我正在寻找一种优雅的方式来反序列化以下输入:
{
"products": [
{
"id": 1,
"ptype": "Clothes",
"description": "some data about clothes",
"metadata": {
"colors" : ["blue", "green"],
"web": false,
"size": 2
}
},
{
"id": 4,
"ptype": "Food",
"description": "text for foods",
"metadata": {
"country": "France",
"wine": true
}
},
{
"id": 12,
"ptype": "EmptyPlaceholder",
"description": "nothing at all",
"metadata": {
}
}
]
}
json 包含一个产品数组。产品可以通过 ptype 字段来标识。根据字段的类型,元数据对象会有所不同。例如,如果 ptype 是 Food,那么 food 的元数据将是一个字符串(国家)和一个布尔值(wine)。所以产品有一些共同的字段,id,ptype和description以及一些元数据。我想在 Vec<Product> 中反序列化这个 JSON 文件。
到目前为止,我使用了以下代码:
use serde::{Deserialize};
use serde_json::Result;
#[derive(Deserialize, Debug)]
struct ClothesData {
colors : Vec<String>,
web : bool,
size: u32,
}
#[derive(Deserialize, Debug)]
struct FoodData {
country: String,
wine: bool,
}
#[derive(Deserialize, Debug)]
struct EmptyData {
}
#[derive(Deserialize, Debug)]
enum Metadata {
ClothesData,
FoodData,
EmptyData,
}
#[derive(Deserialize, Debug)]
enum Ptype {
Clothes,
Food,
EmptyPlaceholder
}
#[derive(Deserialize, Debug)]
struct Product {
id: u32,
ptype: Ptype,
description: Option<String>,
metadata: Metadata,
}
我不确定如何从这里开始,我想问一下 serde crate 是否可以“自动”执行此操作。
【问题讨论】: