9951 explained code solutions for 126 technologies


rustUsing enum in json in Rust


Enums in Rust can be used in JSON by serializing them into strings. To do this, the #[serde(rename = "string")] attribute can be used to specify the string value of the enum. For example, the following code creates an enum with the variants Foo and Bar and serializes them into strings:

#[derive(Serialize, Deserialize)]
#[serde(rename = "string")]
enum MyEnum {
    Foo,
    Bar,
}

The output of this code would be a JSON object with the string values of the enum:

{
    "string": "Foo"
}

The #[serde(rename = "string")] attribute can also be used to specify custom string values for each variant of the enum. For example, the following code creates an enum with the variants Foo and Bar and serializes them into strings with custom values:

#[derive(Serialize, Deserialize)]
#[serde(rename = "string")]
enum MyEnum {
    #[serde(rename = "foo")]
    Foo,
    #[serde(rename = "bar")]
    Bar,
}

The output of this code would be a JSON object with the custom string values of the enum:

{
    "string": "foo"
}

Using enums in JSON in Rust can be a useful way to add type safety to your data. It can also help to make your code more readable and maintainable.

Helpful links

Edit this code on GitHub