json icon indicating copy to clipboard operation
json copied to clipboard

Deserialize empty map into None

Open lexa opened this issue 3 years ago • 1 comments

I have a JSON format which could be either a tagged value or an empty map {}. Examples of JSON:

{"A" : 123}
{"B" : 234}
{}

I want to deserialize it into a enum, so I defined one:

#[derive(Debug, Serialize, Deserialize)]
enum Enum {
    A(i64),
    B(i64),
    #[serde(other)]
    None,
}

Despite having one option labeled with #[serde(other)] de-serialization fails for {} .

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=dd5719ccc279964cc65d545a2261a177

lexa avatar Jul 05 '22 14:07 lexa

This needs to be fixed in serde's Deserialize derive macro, not serde-json. You could repost this issue there: https://github.com/serde-rs/serde/issues/

kangalio avatar May 23 '23 17:05 kangalio