Skip to content

feat(java): allow unpaired UTF-16 surrogates in Fory JSON strings - #4232

Open
pavel-ptashyts wants to merge 2 commits into
apache:mainfrom
pavel-ptashyts:json-unpaired-surrogates
Open

pavel-ptashyts wants to merge 2 commits into
apache:mainfrom
pavel-ptashyts:json-unpaired-surrogates

Conversation

@pavel-ptashyts

@pavel-ptashyts pavel-ptashyts commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Why?

Fory JSON rejects a string or name that holds a UTF-16 surrogate without its partner, both when
reading ("\ud83d") and when writing (a String or char holding a lone surrogate). Such text
appears 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?

  • Builder option. ForyJsonBuilder.allowUnpairedSurrogates(boolean), disabled by default and
    exposed as JsonConfig.allowUnpairedSurrogates(). With the default, behaviour is unchanged.
  • Read. When enabled, every \uXXXX escape decodes on its own, so an escaped lone surrogate
    becomes that single char; a raw lone surrogate in String input is kept. Valid pairs decode to
    the 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).
  • Write. When enabled, a lone surrogate in string and char values and map keys is written as
    a lowercase \uXXXX escape in the String and UTF-8 writers, with and without escapeNonAscii, so
    the output is valid UTF-8 and reads back to the same value.
  • Scope kept by decision. The option is for string data. Raw JSON supplied by the caller and
    names declared in code (@JsonProperty, subtype names), which are escaped and validated once
    when the type metadata is built, keep their existing validation. A name read from input with a
    lone surrogate simply matches no declared property.
  • Compact strings. StringJsonWriter assumes a UTF16-coded input String produces non-Latin1
    output. 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's
    compact-string representation (and String.equals) consistent.
  • Hot paths. The flag is read only inside existing surrogate branches, after a surrogate was
    seen and pairing failed; non-surrogate text and valid pairs keep their current code. No SWAR or
    word-copy loop changed.
  • Docs. Builder and JsonConfig Javadoc; docs/json/object-mapping.md options table and an
    "Unpaired surrogates" section.
  • Tests. 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 in
    String input, strict default still rejects), writeUnpairedSurrogateField (String, UTF-8 and
    pretty output, char values, round trip) and writeUnpairedSurrogates (every writer path for
    String and CharSequence, Latin1 and UTF16 String output, with and without escapeNonAscii).
    Object tests run with codegen on and off. Existing strict tests are unchanged.

Related issues

AI Contribution Checklist

  • Substantial AI assistance was used in this PR: yes
  • If yes, I included a completed AI Contribution Checklist in this PR description and the required AI Usage Disclosure.
  • If yes, my PR description includes the required ai_review summary and screenshot evidence or equivalent persisted links of the final clean AI review results from both fresh reviewers described in AI_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.md section 9:

  • Substantial AI assistance was used in this PR: yes
  • I included the standardized AI Usage Disclosure block below.
  • I can explain and defend all important changes without AI help.
  • I reviewed AI-assisted code changes line by line before submission.
  • I completed line-by-line self-review first and fixed issues before requesting AI review.
  • I ran two fresh AI review agents on the current PR diff or current HEAD after the latest code changes: one Fory-guided reviewer using AGENTS.md and .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.md or any copied Fory-specific review checklist.
  • I addressed all AI review comments and repeated the review loop until both ai reviewers reported no further actionable comments.
  • I attached equivalent persisted links of the final clean AI review results from both fresh reviewers on the current PR head in this PR body.
  • I ran adequate human verification and recorded evidence (checks run locally, pass/fail summary, and confirmation I reviewed results).
  • I added/updated tests and specs where required.
  • I validated protocol/performance impacts with evidence when applicable.
  • I verified licensing and provenance compliance.
AI Usage Disclosure
- substantial_ai_assistance: yes
- scope: design drafting, code drafting, tests, docs, PR text
- affected_files_or_subsystems: java/fory-json (ForyJsonBuilder, JsonConfig, Latin1/UTF-16/UTF-8 readers, String/UTF-8 writers, JsonStringTest), docs/json/object-mapping.md
- ai_review: line-by-line self-review by the contributor is pending (see unchecked items above). Two fresh reviewers (Fory-guided and independent). Round 1: both flagged that names declared in code (@JsonProperty, subtype names) still reject lone surrogates while the docs said property names; resolved by limiting the option to string data and documenting that declared names stay validated. Round 2 on head f8165d06: both reported no further actionable comments.
- ai_review_artifacts: Fory-guided reviewer: https://github.com/apache/fory/pull/4232#issuecomment-6951619121 ; independent general reviewer: https://github.com/apache/fory/pull/4232#issuecomment-6951619860 (both on head f8165d06, final round, no further actionable comments)
- human_verification: see "Verification" below
- performance_verification: the flag is read only after a surrogate fails to pair; non-surrogate and paired paths and all word-copy loops are unchanged; no JMH run
- provenance_license_confirmation: all code written for this PR against the Apache Fory sources; no third-party code introduced

Verification

JDK 25.0.2, Windows 11 x86_64, base apache/main c8bb610:

  • 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?

  • Does this PR introduce any public API change? Yes: ForyJsonBuilder.allowUnpairedSurrogates(boolean) and JsonConfig.allowUnpairedSurrogates(). Default behaviour is unchanged.
  • Does this PR introduce any binary protocol compatibility change?

🤖 Generated with Claude Code

pavel-ptashyts and others added 2 commits November 17, 2026 15:41
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

Copy link
Copy Markdown
Contributor Author

AI review artifact: Fory-guided reviewer (final round)

  • Reviewer: Codex CLI codex exec, model gpt-6-astra, reasoning effort medium, read-only sandbox, fresh session
  • Instructions: Fory-guided reviewer per AI_POLICY.md, following AGENTS.md and .agents/ci-and-pr.md
  • Reviewed: git diff apache/main...HEAD at head f8165d06 (base c8bb6101)
  • Result:
No further actionable comments.

@pavel-ptashyts

Copy link
Copy Markdown
Contributor Author

AI review artifact: independent general reviewer (final round)

  • Reviewer: Claude Code CLI claude -p, model claude-opus-5-5, effort medium, read-only tools (Read Grep Glob), no MCP, fresh session
  • Instructions: independent general reviewer per AI_POLICY.md; not pointed to .agents/ci-and-pr.md or any Fory review checklist
  • Reviewed: git diff apache/main...HEAD at head f8165d06 (base c8bb6101)
  • Result:
No further actionable comments.

This branch has not been deployed

No deployments
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.

[Java] Fory JSON: optional tolerance for unpaired UTF-16 surrogates

1 participant