Open luxas opened 3 years ago
Go also has an open issue for this: https://github.com/golang/go/issues/48298. dsnet wrote a great summary of the problem and I'd suggest a read.
As far as implementation, this looks like it might require another bool on generalStructDecoder:
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: https://github.com/golang/go/issues/24415#issuecomment-373513285):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/)