Home > OS >  What does serde deserialize dashes to?
What does serde deserialize dashes to?

Time:07-25

For a project I'm working on I have to implement a struct for serde to deserialize some JSON data into. The other guys on the project have expressed their great desire that a certain field of the struct be an enum.

Unfortunately, it turns out that one of the possible values in the JSON includes a “-” character. Insofar as I have been able to determine, enum variants can't have dashes in their names.

How do I deserialize data that might contain slashes? I tried looking through the serde documentation, but haven't been able to find anything.

Here's an example of the kind of data I'm talking about:

{
    "fubar": "foo-bar"
}

CodePudding user response:

Use #[serde(rename)]:

enum Foo {
    #[serde(rename = "foo-bar")]
    FooBar,
}
  • Related