Customize serde_json derive on enum


#1

I have an enum that I’m trying to derive JSON serialization and deserialization for using serde:

#[derive(Serialize, Deserialize)]
enum MyEnum {
    Foo,
    Bar,
}

While the names Foo and Bar are idiomatic Rust, I’d like them to correspond to the JSON strings "foo" and "bar". Unfortunately, serde derives serialization and deserialization that have them correspond to "Foo" and "Bar" (upper-cased). Is there a way to instruct the automatic derivation to use the lower-case variants, or will I have to implement the serialization/deserialization by hand?


#2

There is a large set of attributes that control the generated implementations: https://serde.rs/attributes.html


#3

Perfect, thanks! That’s exactly what I need.