Skip to content

Add Decoder.DisallowDuplicateFields to reject duplicate object keys - #606

Open
ChrisJr404 wants to merge 1 commit into
goccy:masterfrom
ChrisJr404:disallow-duplicate-fields
Open

Add Decoder.DisallowDuplicateFields to reject duplicate object keys#606
ChrisJr404 wants to merge 1 commit into
goccy:masterfrom
ChrisJr404:disallow-duplicate-fields

Conversation

@ChrisJr404

Copy link
Copy Markdown

Closes #385.

This adds a DisallowDuplicateFields option on Decoder, matching the existing DisallowUnknownFields. When it is set, decoding into a struct returns an error if an object contains more than one key that maps to the same field, instead of silently letting the last value win. That is useful when you want stricter parsing for compatibility with other JSON implementations (the issue links dsnet's writeup on why duplicate keys are a footgun).

It reuses the seenFields bookkeeping that was already there for DecodeFieldPriorityFirstWin, so the default decode path is unchanged and the map is only allocated when the option is turned on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add Decoder.DisallowDuplicateFields

1 participant