Skip to content

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

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

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.

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.

2 participants