Skip to content

Fix duplicate json and reader path - #3181

Open
masterramankhatri-glitch wants to merge 2 commits into
google:mainfrom
masterramankhatri-glitch:fix-duplicate-json-and-reader-path
Open

masterramankhatri-glitch wants to merge 2 commits into
google:mainfrom
masterramankhatri-glitch:fix-duplicate-json-and-reader-path

Conversation

@masterramankhatri-glitch

@masterramankhatri-glitch masterramankhatri-glitch commented Nov 20, 2026 •

Copy link
Copy Markdown

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

import com.google.gson.*;

public class DuplicateKeyDemo {
  public static void main(String[] args) {
    String json = "{\"id\": 1, \"id\": 2}";

    JsonElement result = JsonParser.parseString(json);
    System.out.println(result); // {"id":2} - no error, first value is lost

    // Same inconsistency for a POJO target: the field is set twice, last value wins.
    // Gson gson = new Gson();
    // IdHolder holder = gson.fromJson(json, IdHolder.class);
  }
}

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.:

JsonSyntaxException: duplicate name: "id"

(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

  1. Silent last-value-wins can mask corrupted / hand-edited / merged JSON and cause hard-to-debug data loss.
  2. It is inconsistent: deserializing the same JSON into a Map already throws JsonSyntaxException: duplicate key: ..., while deserializing into a JsonElement or POJO does not.
  3. RFC 8259 states object names SHOULD be unique; a parser silently discarding values makes violations undetectable for callers.

Proposed approach

  • Reject duplicate raw object names by default, independently of Strictness - strictness controls JSON syntax leniency, while duplicate-name handling is a separate policy, and entry points like JsonParser (which parses leniently) should not disagree with Gson.fromJson().
  • Track names per-object centrally in JsonReader as property-name tokens are recognised, covering all name routes (e.g. nextName(), names promoted for Map keys via promoteNameToValue(), and selectName() if used), not just one method.
  • Keep the existing Map duplicate check: it also catches distinct raw names that convert to equal typed keys (e.g. "1" and "01" -> Integer 1) and duplicates in Map-as-array form.
  • Out of scope: 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

  • Gson version: 2.13.2
  • Java version: 21.0.5
  • OS: Windows 11

This branch has not been deployed

No deployments
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.

JsonParser / Gson silently accepts duplicate JSON object names - should reject like duplicate Map keys

1 participant