serde-rs / json

Strongly typed JSON library for Rust
Apache License 2.0
4.7k stars 536 forks source link

Checking for duplicate keys #1112

Open jfehr67 opened 4 months ago

jfehr67 commented 4 months ago

By default, serde json will parse a json stream into a Value and for any duplicate keys, simply use the last value. (Just calls the insert function in the Map<String, Value>, which does this.)

In our use case, we sometimes have quite large json files that multiple developers add entries to. The entire json isn't a struct, but we do pull out portions which we then deserialize to structs. I'd like to be able to get an error if there are duplicate keys so the developer knows to update the original entry.

The optimal place would be in the deserialize function in map.rs, ie:

               while let Some((key, value)) = tri!(visitor.next_entry()) {
                   if (values.contains_key(key)) {
                        return Err(de::Error::duplicate_field(key));
                    }
                    values.insert(key, value);
                }

Would this be a reasonable solution? I'm not sure how to add/pass parameters to the deserializer that would enforce/not enforce this and I'm sure this isn't behavior everyone would want, but I'd prefer to contribute this back to the repo instead of keeping modified fork.

jfehr67 commented 4 months ago

Submitted a PR here: https://github.com/serde-rs/json/pull/1113