Skip to content

Checking for duplicate keys #1112

Open
Open
@jfehr67

Description

@jfehr67

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions