Customize serde_json derive on enum

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?

There is a large set of attributes that control the generated implementations: Attributes · Serde

2 Likes

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