How to represent unknown variant `` with serde?

I need to deserialize an Object where a field can also be an empty String. Does serde have something to handle this case?

    #[derive(serde::Deserialize, Debug)]
    pub enum Test {
        
        A,
        B,
        C,
        // "", <<<<<<<<<<<<
    }
reqwest::Error {
        kind: Decode,
        source: Error("unknown variant ``,
1 Like

rename looks like it works with empty strings (it does have to be a string though).

#[derive(serde::Deserialize, Debug)]
pub enum Test {
    A,
    B,
    C,
    #[serde(rename = "")]
    Empty,
}
1 Like

it worked! I found so many complicated solutions on google. This is great. thx

But why using Empty and not None?

There's no particular reason to call the variant None. If you did, that wouldn't make it have anything to do with Option::None, and it might cause confusion. But if you want to, you can.

1 Like