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
JSON's RFC4627 requires that mappings keys merely “SHOULD” be unique, while YAML insists they “MUST” be. Technically, YAML therefore complies with the JSON spec, choosing to treat duplicates as an error. In practice, since JSON is silent on the semantics of such duplicates, the only portable JSON files are those with unique keys, which are therefore valid YAML files.
As the recommendation of JSON specifies that fields SHOULD be unique; it would be nice to have an option to enforce this; especially for consistency between JSON and YAML parsers (e.g. both would return errors on duplicate fields consistently).
yaml.v3 errors out if there are duplicate fields. It can be very non-obvious how parsing is done if duplicate fields don't yield an error, e.g. encoding/json where duplicate fields with object values are merged, whereas all other types are replaced with the latest value (ref: golang/go#24415 (comment)):
The documentation doesn't specify what happens in this case; it says it matches object keys to fields but doesn't specify in what order it does so (it only matters with duplicate keys). Changing the current behaviour might break backwards compatibility too much, but I think at least it should be documented.
Also, the behaviour is inconsistent between a map and a slice field; the maps get merged, the slice field takes the last value seen, like the other types.
But, I think it should be possible to write this as a json-iter extension as well; I don't know which one is better for the community in the long run (i.e. feature creep vs extensibility).
If json-iter supported "warnings", it'd also be possible to begin the transition to duplicate fields erroring by showing the user warnings (in the spirit of https://kubernetes.io/blog/2020/09/03/warnings/)
The text was updated successfully, but these errors were encountered:
Related to #570
As can be read in YAML 1.2 spec :
As the recommendation of JSON specifies that fields
SHOULD
be unique; it would be nice to have an option to enforce this; especially for consistency between JSON and YAML parsers (e.g. both would return errors on duplicate fields consistently).yaml.v3
errors out if there are duplicate fields. It can be very non-obvious how parsing is done if duplicate fields don't yield an error, e.g.encoding/json
where duplicate fields with object values are merged, whereas all other types are replaced with the latest value (ref: golang/go#24415 (comment)):Here is that comment behavior in action: https://play.golang.org/p/CkCMdoGXc07
This was unexpected to me, and I believe most users, too.
I have an implementation of this fix, providing an opt-in method to enable errors on duplicate fields: https://github.com/luxas/json-iterator/tree/error_for_duplicates
But, I think it should be possible to write this as a
json-iter
extension as well; I don't know which one is better for the community in the long run (i.e. feature creep vs extensibility).If
json-iter
supported "warnings", it'd also be possible to begin the transition to duplicate fields erroring by showing the user warnings (in the spirit of https://kubernetes.io/blog/2020/09/03/warnings/)The text was updated successfully, but these errors were encountered: