Repository navigation
Conversation
Gson.fromJson(JsonElement, ...) creates a JsonTreeReader without applying the strictness configured on the Gson instance, unlike the Writer-based toJson paths and newJsonReader. As a result, a Gson instance configured with Strictness.STRICT rejected JSON documents containing unpaired UTF-16 surrogates when read from a string or reader (since google#3116), but silently accepted the exact same documents when passed as a JsonElement, for both string values and member names. Two changes: - Gson.fromJson(JsonElement, TypeToken) now applies the strictness of the Gson instance (LEGACY_STRICT when none is set, matching newJsonReader), so the strictness setting is consistent between all fromJson overloads. - JsonTreeReader.nextString and nextName now reject strings containing unpaired surrogates in STRICT mode, mirroring the validation the stream-based JsonReader performs (validateString). Non-strict behavior is unchanged. Signed-off-by: simpleqt <2015900818@qq.com>
|
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. |
|
Closing this one rather than leaving it open for something that cannot happen: the Created by AI (DeepSeek Harness, model dsv41). |
Purpose
Gsoninstances configured withStrictness.STRICTdo not enforce strict mode when deserializing from aJsonElement: documents containing unpaired UTF-16 surrogates that are correctly rejected byfromJson(String, ...)/fromJson(Reader, ...)(behavior introduced in #3116) are silently accepted byfromJson(JsonElement, ...)- both as string values and as object member names.Closes no existing issue (not currently tracked).
Reproduction
Root cause
Two gaps in the tree-reading path:
Gson.fromJson(JsonElement, TypeToken)creatednew JsonTreeReader(json)without applying the strictness of theGsoninstance. UnlikenewJsonReader(Reader)and the writer paths (toJson(JsonElement, JsonWriter)etc.), the reader created for the tree never received the configured strictness, so it kept the defaultLEGACY_STRICT.JsonTreeReader, itsnextString()/nextName()returned strings from the tree without the unpaired-surrogate validation (validateString) that the stream-basedJsonReaderperforms since fix: reject unpaired surrogates in strict JSON #3116.Fix
Gson.fromJson(JsonElement, TypeToken)now applies the strictness of theGsoninstance to theJsonTreeReader(LEGACY_STRICTwhen no strictness is set, matchingnewJsonReader), making the strictness behavior consistent across allfromJsonoverloads.JsonTreeReader.nextString()andnextName()now reject strings containing unpaired surrogates inSTRICTmode, mirroringJsonReader.validateString. This follows the existing precedent ofvalidateAscii, which is likewise implemented in both reader classes. Non-strict behavior (default,LENIENT, andLEGACY_STRICT) is unchanged, and paired surrogates are accepted as before.Tests
Added to
JsonTreeReaderTest: strict mode rejects unpaired surrogate in string value and in member name; strict mode accepts paired surrogates; non-strict mode accepts unpaired surrogates.Added to
GsonTest: strictGsonthrowsJsonSyntaxExceptionforfromJson(JsonElement, ...)with an unpaired surrogate; defaultGsonstill accepts it.Red/green evidence (JDK 17.0.11, Maven 3.9.9), identical test files in both runs, only the production sources differ:
main@ 5773104) + tests:mvn -pl gson test -Dtest='JsonTreeReaderTest,GsonTest'Tests run: 37, Failures: 3, Errors: 0- all three new strict-mode tests fail withexpected ... to be thrown, but nothing was thrown.Tests run: 37, Failures: 0, Errors: 0mvn -pl gson test->Tests run: 4683, Failures: 0, Errors: 0, Skipped: 20mvn -pl gson spotless:check-> BUILD SUCCESSChecklist
mvn spotless:checkpasses)mvn -pl gson testpasses without errors (full module: 4683 tests, 0 failures)Note
This pull request was prepared with AI assistance (AI-generated diagnosis and patch, human-reviewed); the change, tests, and verification steps above were run locally against
mainas described.