Skip to content

JsonReader / JsonTreeReader.promoteNameToValue() causes incorrect JSON path #1768

Closed

Description

@Marcono1234

The internal method JsonReaderInternalAccess.promoteNameToValue(JsonReader) which is used before the key of a Map<K,V> is read causes an incorrect JSON path because after the name has been read as value, the JSON path is not updated.

For example:

String malformedJson = "{\"name\":\"value";
try {
    new Gson().fromJson(malformedJson, new TypeToken<Map<String, String>>() {}.getType());
} catch (JsonSyntaxException exception) {
    // Incorrect: Path is missing name
    // Unterminated string at line 1 column 15 path $.
    System.out.println(exception.getCause().getMessage());
}

JsonReader jsonReader = new JsonReader(new StringReader(malformedJson));
jsonReader.beginObject();
jsonReader.nextName();
try {
    jsonReader.nextString();
} catch (MalformedJsonException exception) {
    // Correct: Path includes name
    // Unterminated string at line 1 column 15 path $.name
    System.out.println(exception.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions