Skip to content

fix: allow empty Map/Seq to deserialize as unit struct #32

Open
Adjective-Object wants to merge 2 commits intorushmorem:mainfrom
Adjective-Object:adjective-object/fix-empty-map-seq-unit-struct
Open

fix: allow empty Map/Seq to deserialize as unit struct #32
Adjective-Object wants to merge 2 commits intorushmorem:mainfrom
Adjective-Object:adjective-object/fix-empty-map-seq-unit-struct

Conversation

@Adjective-Object
Copy link

This is part of a series of small fixes bringing serde-content in line with serde's value visitor, to deal with edge-cases.

serde's ContentDeserializer allows empty maps/sequences to deserialize as
unit structs. This is needed for internally tagged enums with unit struct
variants:

#[derive(Deserialize)]
struct Info;

#[derive(Deserialize)]
#[serde(tag = "topic")]
enum Message { Info(Info) }

Where {"topic":"Info"} should deserialize even though unit structs
ordinarily don't deserialize from empty map/seq.

Also changed to fall through to deserialize_any for non-matching cases,
matching serde's behavior.

Reference: https://github.com/serde-rs/serde/blob/d17902059e77e371d8a7f83ff403f9e760b70f45/serde/src/private/de.rs#L1323-L1348

serde's ContentDeserializer allows empty maps/sequences to deserialize as
unit structs. This is needed for internally tagged enums with unit struct
variants:

    #[derive(Deserialize)]
    struct Info;

    #[derive(Deserialize)]
    #[serde(tag = "topic")]
    enum Message { Info(Info) }

Where {"topic":"Info"} should deserialize even though unit structs
ordinarily don't deserialize from empty map/seq.

Also changed to fall through to deserialize_any for non-matching cases,
matching serde's behavior.

Reference: https://github.com/serde-rs/serde/blob/d17902059e77e371d8a7f83ff403f9e760b70f45/serde/src/private/de.rs#L1323-L1348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant