Repository navigation
feat(java): allow unpaired UTF-16 surrogates in Fory JSON strings - #4232
Open
pavel-ptashyts wants to merge 2 commits into
Open
pavel-ptashyts wants to merge 2 commits into
pavel-ptashyts wants to merge 2 commits into
Conversation
Fory JSON rejects a string or name holding a surrogate without its partner on both read and write. Producers that truncate text by UTF-16 length emit such strings, and other Java JSON libraries accept them. Add ForyJsonBuilder.allowUnpairedSurrogates(boolean), disabled by default. When enabled, readers decode each \uXXXX escape on its own and keep raw lone surrogates in String input; UTF-8 input must still be valid UTF-8. Writers emit a lone surrogate as a lowercase \uXXXX escape in String and UTF-8 output. The flag is read only after pairing fails, so non-surrogate text and valid pairs keep their current paths. StringJsonWriter rechecks LATIN1 fitness once in toJson after escaping a lone surrogate, so a UTF16-coded input whose only non-Latin1 char was escaped still yields a compact LATIN1 String. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Names declared in code are escaped and validated when type metadata is built, so they keep strict validation. State that in the builder and JsonConfig Javadoc and the JSON guide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pavel-ptashyts
requested review from
PragmaTwice,
chaokunyang and
theweipeng
as code owners
November 17, 2026 14:54
Contributor
Author
|
AI review artifact: Fory-guided reviewer (final round)
|
Contributor
Author
|
AI review artifact: independent general reviewer (final round)
|
This branch has not been deployed
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.
Why?
Fory JSON rejects a string or name that holds a UTF-16 surrogate without its partner, both when
reading (
"\ud83d") and when writing (aStringorcharholding a lone surrogate). Such textappears in practice when producers truncate strings by UTF-16 length. Other Java JSON libraries
accept it, so services migrating to Fory JSON start failing on these payloads, and a write failure
on a response becomes a server error. Details in #4231.
What does this PR do?
ForyJsonBuilder.allowUnpairedSurrogates(boolean), disabled by default andexposed as
JsonConfig.allowUnpairedSurrogates(). With the default, behaviour is unchanged.\uXXXXescape decodes on its own, so an escaped lone surrogatebecomes that single
char; a raw lone surrogate inStringinput is kept. Valid pairs decode tothe same two chars as before. This covers value decoding, validation scans and skipping, field
name hashing, map keys, and subtype discriminator scanning in the Latin1, UTF-16 and UTF-8
readers. UTF-8 byte input must still be valid UTF-8 (an encoded surrogate is still rejected).
charvalues and map keys is written asa lowercase
\uXXXXescape in the String and UTF-8 writers, with and withoutescapeNonAscii, sothe output is valid UTF-8 and reads back to the same value.
names declared in code (
@JsonProperty, subtype names), which are escaped and validated oncewhen the type metadata is built, keep their existing validation. A name read from input with a
lone surrogate simply matches no declared property.
StringJsonWriterassumes a UTF16-coded inputStringproduces non-Latin1output. An escaped lone surrogate can be its only non-Latin1 char, so the writer records that it
escaped one and
toJson()rechecks once whether the result fits LATIN1, keeping the JDK'scompact-string representation (and
String.equals) consistent.seen and pairing failed; non-surrogate text and valid pairs keep their current code. No SWAR or
word-copy loop changed.
JsonConfigJavadoc;docs/json/object-mapping.mdoptions table and an"Unpaired surrogates" section.
JsonStringTest:readUnpairedSurrogates(escaped and raw lone surrogates in Latin1,UTF-16 and UTF-8 input, including decode tails after an escape and long runs; encoded surrogate
bytes still rejected; quoted-text decoding of a temporal/UUID value fails cleanly),
readUnpairedSurrogateNames(unknown names, map keys, subtype discriminator scan, raw names inString input, strict default still rejects),
writeUnpairedSurrogateField(String, UTF-8 andpretty output,
charvalues, round trip) andwriteUnpairedSurrogates(every writer path forStringandCharSequence, Latin1 and UTF16 String output, with and withoutescapeNonAscii).Object tests run with codegen on and off. Existing strict tests are unchanged.
Related issues
AI Contribution Checklist
yesyes, I included a completed AI Contribution Checklist in this PR description and the requiredAI Usage Disclosure.yes, my PR description includes the requiredai_reviewsummary and screenshot evidence or equivalent persisted links of the final clean AI review results from both fresh reviewers described inAI_POLICY.md, the Fory-guided reviewer and the independent general reviewer, on the current PR diff or current HEAD after the latest code changes.Completed checklist from
AI_POLICY.mdsection 9:yesAI Usage Disclosureblock below.AGENTS.mdand.agents/ci-and-pr.md, and one independent general reviewer in a separate clean-context review session that was not pointed to.agents/ci-and-pr.mdor any copied Fory-specific review checklist.Verification
JDK 25.0.2, Windows 11 x86_64, base
apache/mainc8bb610:java/fory-json:mvn test: 1339 tests, 0 failures, 0 errors.JsonStringTest: 71 tests, 0 failures (object tests with codegen on and off).java/fory-json:mvn spotless:apply: no changes.docs/json/object-mapping.md: prettier clean.Does this PR introduce any user-facing change?
ForyJsonBuilder.allowUnpairedSurrogates(boolean)andJsonConfig.allowUnpairedSurrogates(). Default behaviour is unchanged.🤖 Generated with Claude Code