2 ms·
How would you express deserializing these 'polymorphic JSON' objects using serde/Rust?
by q3k 6y ago
How would you express deserializing these 'polymorphic JSON' objects using serde/Rust?
- speedgoose 6y agoUsing a rust enum I guess.
- q3k 6y agoYou're right, this does seem to work [1]. I wasn't aware that serde would attempt to deserialize multiple enum variants until something matches. [1] - https://serde.rs/enum-representations.html#untagged https://serde.rs/enum-representations.html#untagged
- masklinn 6y agoYeah that's a rather common scheme out there so Serde does provide built-in support for this deserialisation. Probably better for deserialisation performances to use properly tagged enums, but if you don't have a choice Serde's got your back.