I'm trying to implement a way to serialize an enum with various structs into json, but running into some issues with using the flatten command. Here's my enum:
enum E {
Type1,
Type2 {
foo: String
},
Type3 {
foo: String,
bar: String
}
}
I'd like to find a way to serialize this into something like:
// Type 1
{type: "1"}
//Type 2 (with foo = "A")
{type: "2", foo: "A"}
//Type 3 (with foo = "A" and bar = "B")
{type: "3", foo: "A", bar: "B"}
Is there anyway to do this with the built in attributes? I can generate the field type by adding a tag to the enum, and I can rename the enum-structs with the rename attribute, but can't figure out how to flatten the enum-structs
Just pointing out that I came across this earlier post, which is similar, but only covers the deserialization step: Serde_json: Flatten nested internally-tagged enums?
It is also a bit old now, so wasn't sure if things have changed since then
In combination with #[serde(rename = "1")], #[serde(rename = "2")], etc. on each enum variant, I think you should be able to get your desired behavior.
Ah, yeah, that should totally work actually. For some reason I was thinking that I needed Type1 to expand to {type: "1", foo: "", bar: ""} to denote the missing field, but I don't. And I also didn't include that part in my original post which wasn't very helpful.