Skip to content

Fix: reject trailing data after top-level JSON null#3013

Closed
Senrian wants to merge 1 commit intogoogle:mainfrom
Senrian:fix-trailing-null-v2
Closed

Fix: reject trailing data after top-level JSON null#3013
Senrian wants to merge 1 commit intogoogle:mainfrom
Senrian:fix-trailing-null-v2

Conversation

@Senrian
Copy link
Copy Markdown

@Senrian Senrian commented Apr 15, 2026

Summary

Fixes #3008

Gson.fromJson(String/Reader, ...) and JsonParser.parseReader(Reader) documentedly reject trailing data, but both made an exception when the parsed top-level value was JSON null.

For non-null values, trailing data already causes a JsonSyntaxException. For top-level null, the trailing-data check was skipped.

Changes

Gson.java

// Before:
if (obj != null && reader.peek() != JsonToken.END_DOCUMENT) {

// After:
if (obj == null || reader.peek() != JsonToken.END_DOCUMENT) {

JsonParser.java

// Before:
if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {

// After:
if (jsonReader.peek() != JsonToken.END_DOCUMENT) {

Tests

Added testParseTrailingDataAfterNull() and testFromJsonTrailingDataAfterNull() in JsonParserTest.java to verify trailing data is rejected after parsing top-level null.

Fixes google#3008

Both Gson.fromJson(Reader, ...) and JsonParser.parseReader(Reader)
documentedly reject trailing data, but both made an exception when
the parsed top-level value was JSON 'null'.

Changes:
- Gson.assertFullConsumption(): if obj==null || peek()!=END_DOCUMENT
- JsonParser.parseReader(): removed !element.isJsonNull() guard

Added test cases in JsonParserTest.java.
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 15, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Marcono1234
Copy link
Copy Markdown
Contributor

Please explain how your PR differs from or is better than PR #3008 (which you claim your PR "fixes").

In particular it seems your PR has two problems:

  • your change to use obj == null now fails even if JSON null has no trailing data
  • your test which excepts JsonParser.parseString("nulltrailing") to fail most likely does not work because Gson instead parses that as unquoted string (I assume)

@Senrian Senrian closed this Apr 17, 2026
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.

3 participants