Fix trailing data accepted after top-level null#3008
Open
andrewstellman wants to merge 3 commits intogoogle:mainfrom
Open
Fix trailing data accepted after top-level null#3008andrewstellman wants to merge 3 commits intogoogle:mainfrom
andrewstellman wants to merge 3 commits intogoogle:mainfrom
Conversation
Marcono1234
reviewed
Apr 5, 2026
fix comment Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
fix comment Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Member
|
(Just an update here. I tried running this past Google's internal tests and I did encounter at least one failure. Unfortunately debugging the failure was not straightforward so I don't yet know if this is something that really would be affected by the behaviour change.) |
Senrian
pushed a commit
to Senrian/gson
that referenced
this pull request
Apr 15, 2026
Fixes google#3008 Both Gson.fromJson(Reader, ...) and JsonParser.parseReader(Reader) documentedly reject trailing data, but made an exception when the parsed top-level value was JSON 'null'. This change removes the exception so trailing data is consistently rejected regardless of the parsed value. Changes: - Gson.assertFullConsumption(): Changed condition from 'obj != null && reader.peek() != END_DOCUMENT' to 'obj == null || reader.peek() != END_DOCUMENT' - JsonParser.parseReader(): Removed '!element.isJsonNull() &&' guard Added test cases in JsonParserTest.java to verify trailing data is rejected after parsing top-level null.
Senrian
pushed a commit
to Senrian/gson
that referenced
this pull request
Apr 15, 2026
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.
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.
Summary
Gson.fromJson(String/Reader, ...)andJsonParser.parseReader(Reader)documentedly reject trailing data, but both currently make an exception when the parsed top-level value is JSONnull.For non-null values, trailing data already causes a
JsonSyntaxException. For top-levelnull, the trailing-data check is skipped:Gson.java,assertFullConsumption()only peeks when the deserialized object is non-null.JsonParser.java,parseReader(Reader)only peeks when the parsedJsonElementis notJsonNull.That means inputs like
null trailingare silently accepted by the high-level "consume the whole document" APIs, even though those methods document that trailing data should be rejected.Fix
Apply the trailing-data check regardless of whether the parsed value is
null, while still preserving Gson's existing backward-compatible handling of empty or whitespace-only input.Concretely:
obj != null/!element.isJsonNull()guardsreader.peek()/jsonReader.peek()to verifyEND_DOCUMENTEOFExceptionfrom that final peek and treat it as "input fully exhausted"That
EOFExceptionhandling is important because these APIs have long accepted empty or whitespace-only input asnull/JsonNull, and this change should not alter that behavior.This change only affects the high-level wrappers which are supposed to consume the entire document:
Gson.fromJson(String/Reader, ...)JsonParser.parseReader(Reader)The lower-level streaming entry points:
Gson.fromJson(JsonReader, ...)JsonParser.parseReader(JsonReader)intentionally allow trailing data and are unchanged.
Example
Tests
Three regression tests cover the affected high-level APIs:
GsonTest.testFromJsonRejectsTrailingDataAfterNull_StringGsonTest.testFromJsonRejectsTrailingDataAfterNull_ReaderJsonParserTest.testParseReaderRejectsTrailingDataAfterNullThe
Stringoverload test and theJsonParsertest also verify the existing non-null baseline ("hello" trailing) still throws.Focused compatibility checks also confirm that the fix preserves accepted empty-input behavior:
JsonParserTest.testParseEmptyWhitespaceInputObjectTest.testEmptyStringDeserializationA clean full
gsonmodule test run passes with:mvn -pl gson clean test