You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the latest version of serde_json I seem to have found an issue when round tripping an untagged enum struct variant with similarly named fields. Here is a playground you can try. What am I missing here?
/*[dependencies]serde = { version = "1.0.164", features = ["derive", "rc"] }serde_json = "1.0.97"*/use serde::{Deserialize,Serialize};#[derive(Debug,Serialize,Deserialize,Clone)]pubstructPropertyBinding{pubfieldA:String,pubfieldB:String,}#[derive(Debug,Serialize,Deserialize,Clone)]#[serde(untagged)]pubenumMyEnum{VariantA{value:(f64,f64,f64),},VariantB{value:(f64,f64,f64),inner:PropertyBinding,},}implMyEnum{/// Create new property state bound to the given namespace and property.pubfnvariantb(fieldA:String,fieldB:String,value:(f64,f64,f64)) -> Self{Self::VariantB{
value,inner:PropertyBinding{
fieldA,
fieldB,},}}}fnmain(){let state = MyEnum::variantb("namespace".to_string(),"property".to_string(),(5.0,5.0,5.0));let data = serde_json::to_string_pretty(&state).unwrap();println!("{}", data);let state:MyEnum = serde_json::from_str(&data).unwrap();println!("{:?}", state);assert!(matches!(state, MyEnum::VariantB{ .. }));// << Paincs}
Output:
{
"value": [
5.0,
5.0,
5.0
],
"inner": {
"fieldA": "namespace",
"fieldB": "property"
}
}
VariantA { value: (5.0, 5.0, 5.0) }
thread 'main' panicked at 'assertion failed: matches!(state, MyEnum :: VariantB { .. })', src/main.rs:48:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
The text was updated successfully, but these errors were encountered:
paxsonsa
changed the title
Round Trip of Enum Struct Variant is Failing
Round Trip of Untagged Enum Struct Variant is Failing
Jun 23, 2023
Correct solution is to swap the order of the variants, because VariantA is a subset of VariantB. Untagged enums tries each variant in the declaration order until deserialization would be successful. But VariantA can be successfully deserialized from any VariantB representation
Thanks for taking the time to respond! I wasn't aware that the ordering of variants affected the deserialization! I must have missed that in the docs! Good to know!
That strikes me as something that could slip up some APIs over time as new variants are added to a enum as that behaviour is not explicitly clear reading the code which is why I opt'd for deny_unknown_fields. That is not to say your idea is wrong by any means just less ideal for my situation.
Hey there,
Using the latest version of
serde_json
I seem to have found an issue when round tripping an untagged enum struct variant with similarly named fields. Here is a playground you can try. What am I missing here?https://www.rustexplorer.com/b/2w9j4u
Example code:
Output:
The text was updated successfully, but these errors were encountered: