Hi, I'm trying to deserialize a json like this:
{
"field1": {
"type": "type1",
"id":"field1-id"
},
"att1": true,
"att2": "A"
}
Another example:
{
"field1": {
"type": "type2",
"id":"field1-id"
},
"att3": "aaaa",
"att4": 123
}
The basic gist of this being: the main json object has always a "field1" object, and one of its properties defines the main object's "type", which in turn determines a number of extra properties.
I thought about implementing with a struct, an enum tagged on "type" with variants to be flattened into the main struct:
pub struct Field1 {
#[serde(rename = "type")]
pub field_type: String,
pub id: String,
}
pub struct MyStruct {
pub field1: Field1,
#[serde(flatten)]
pub variant: Type,
}
#[serde(tag = "type")]
pub enum Type {
Unknown,
Type1 {
att1: bool,
att2: String,
},
Type2 {
att3: String,
att4: i32,
},
}
But of course this doesn't work, as "type" is not part of the enum to be tagged, so my question is, is it possible to provide a custom way of determining the correct variant, or perhaps this approach is completely wrong and there's another way?
Thank you for your help!