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
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.
The text was updated successfully, but these errors were encountered:
Looking at #1113, the check can be activated at compile-time.
But according to this question on StackOverflow, duplicate keys are not forbidden by standards, even if not recommended. If I understand the discussion correctly, the interpretation of whether a duplicate key is an issue or not is subject-dependent.
So maybe the check should be activated at runtime?
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:
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.
The text was updated successfully, but these errors were encountered: