Serde with varying name depending on enum?

Hi,

The playground:https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=0c962818dd458e9f0ecccd5424be9736

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 ?

Thanks,

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

1 Like

Yes! that was it!

Thanks a lot jofas

1 Like