Given this enum definition:
#[repr(u8)]
#[derive(Debug, Serialize)]
pub enum AnimalType {
#[serde(rename = "cat")]
Cat = 1,
#[serde(rename = "dog")]
Dog = 2,
#[serde(rename = "snake")]
Snake = 3,
#[serde(rename = "capybara")]
Capybara = 4,
}
I'd like to be able to return JSON defining the above enum similar to this:
{
"AnimalType": [
{ "cat": 1 },
{ "dog": 2 },
{ "snake": 3 },
{ "capybara": 4 }
]
}
Is it possible to do this programmatically in Rust, possibly with serde?
This question differs from How do I configure Serde to use an enum variant's discriminant rather than name? as I want to serialize the definition of the enum, not a value.