I have JSON which takes two forms:
"Example:" { "field": 42, "A": 76 }
"Example:" { "field": 42, "B": 110 }
I want to deserialize it into a struct like this:
struct Example {
field: i32,
an_enum: AnEnum,
}
where
enum AnEnum {
A(i32),
B(i32),
}
I don't know how to do it without writing a custom deserializer for Example
.
This works:
"Example:" { "field": 42, "an_enum": {"A": 76} }
or, in YAML:
Example:
field: 42
an_enum:
A: 76
The an_enum
is superfluous and annoying to write. How can I deserialize the first form into the struct? Or, alternatively, how can I declare a struct that will successfully deserialize the syntax I want?
You are looking for
#[serde(flatten)]
:Before this was available, I'd use custom deserialization: