Repository navigation
Fix duplicate json and reader path - #3181
Open
masterramankhatri-glitch wants to merge 2 commits into
Open
masterramankhatri-glitch wants to merge 2 commits into
masterramankhatri-glitch wants to merge 2 commits into
Conversation
…ld reject like duplicate Map keys
9 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3179
Describe the issue
Gson silently accepts duplicate names in a JSON object and keeps only the last value. This hides data errors and is inconsistent with Gson's existing behaviour for
Map, where duplicate keys are already rejected.Steps to reproduce
Current behaviour
{"id": 1, "id": 2}parses successfully as{"id": 2}. No warning or exception.Expected behaviour
Parsing should fail with a clear error identifying the duplicate name, e.g.:
(MalformedJsonException at the JsonReader level, converted to JsonSyntaxException by Gson's public parsing paths, consistent with how other malformed JSON is reported.)
JSON with distinct names must continue to parse exactly as before, including case-different names, which are distinct in JSON:
{"a": 1, "A": 2}Why this matters
Mapalready throwsJsonSyntaxException: duplicate key: ..., while deserializing into aJsonElementor POJO does not.Proposed approach
Strictness- strictness controls JSON syntax leniency, while duplicate-name handling is a separate policy, and entry points likeJsonParser(which parses leniently) should not disagree withGson.fromJson().JsonReaderas property-name tokens are recognised, covering all name routes (e.g.nextName(), names promoted for Map keys viapromoteNameToValue(), andselectName()if used), not just one method."1"and"01"->Integer 1) and duplicates in Map-as-array form.JsonObject.add()- that is a programmatic tree-mutation API with replace semantics, not a JSON parser.Compatibility note
This is a deliberate behaviour change: callers relying on last-value-wins for repeated names will now receive an error instead. That is the intended fix, and it aligns
JsonElement/ POJO parsing with the existing Map behaviour.Environment