Serde with varying name depending on enum?


The playground:

What I want is to have the Element::field renamed to the enum variant selected.

Reading the tagged enums didn't help me.
So, for

let unit = Element{other : 0, field : ElementEnum::Type1("Salut".to_string())};

I should end up with:

{"other":0, "NewNameString":"Salut"}

the "field" should be repalced with the name of the selected enum.

How to make this happen ?


#[serde(flatten)] on field should do what you want: Rust Playground

1 Like

Yes! that was it!

Thanks a lot jofas

1 Like