-
-
Notifications
You must be signed in to change notification settings - Fork 790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute to control behavior when deserializing duplicate fields #690
Comments
It might make sense to only change |
I would prefer to address this in serde_derive. That makes the behavior configurable, performant, and consistent across all formats. This is the relevant part of the generated code: |
Sure, it just feels to me that "This format transparently allows duplicate keys" is a a property of the particular serialization format. It shouldn't be expected to be consistent across formats, and not something the user should have to make a decision on for each of their deserializable structs. The decision might even have different answers for different formats - it can be legal to allow in JSON, but not legal to allow for some other format. Aside: Deserializing into a |
Agreed, and this is one of the common tradeoffs we make in Serde all the time. An existing example is The fact that Serialize/Deserialize allows your data to be represented in any Serde format is nice, but not because people actually use it that way. The vast majority of Serialize structs are only ever used with a single format, as is the case for the one you are dealing with. The reason it is nice for Serialize/Deserialize to support every format is that the techniques you learn in one format are transferable across the rest of the ecosystem. When you eventually use a different format for a different project, there is very little you need to learn or re-learn. That is one reason to handle these things with attributes in serde_derive. |
That makes sense. |
Came across this issue and felt like I should throw in a fourth possibility. I have data format akin to JSON (but following JSON is 100% valid) where duplicate fields should be aggregated into a {
"core": "FOO",
"core": "BAR"
} #[derive(Deserialize, Debug)]
struct Cores {
core: Vec<String>,
// other fields ...
} |
Thanks! I added it to the list. |
This should be implemented outside of Serde as a Deserializer adapter -- similar to how serde-ignored or serde-stacker wrap an existing Deserializer into their own Deserializer with extra behavior. Possible usage: let mut j = serde_json::Deserializer::from_str(input);
let de = WhenDuplicateField::keep_first(&mut j);
let t = T::deserialize(de)?; |
If there ever exists a crate that would allow rejecting deserialization of data with duplicate keys, please ping me. |
I believe you can get that behavior (error on duplicate key) if you deserialize into a struct. The repo linked in #1716 has a test that demonstrates this. The error looks like |
@tones111 I see. But it isn't there for collections, like |
Hmmm... Now that I think about it ... couldn't we just have a
impl it for the default collections that serde currently supports, and change serde to insert via this trait, so then people could just write a NewTypes that implement I probably don't know what I'm talking about, but just crossed my mind. |
@dpc I think what you're looking for is implemented in serde_with check out maps_duplicate_key_is_error |
@tones111 Thank you! |
cc @Arnavion
The text was updated successfully, but these errors were encountered: