Repository navigation
feat(java): add JsonReader.readRawValue for custom JSON codecs - #4230
Open
pavel-ptashyts wants to merge 4 commits into
Open
pavel-ptashyts wants to merge 4 commits into
pavel-ptashyts wants to merge 4 commits into
Conversation
Custom codecs can read the next JSON value as its exact source text, the read-side counterpart of @JsonRawValue and writeRawValue(String). The value is validated by skipValue, including string escapes, UTF-8 input and the configured maximum depth. Utf8JsonReader decodes the span as UTF-8 because its ASCII-only slice cannot carry string content. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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:46
Contributor
Author
|
AI review artifact: Fory-guided reviewer (final round)
|
Contributor
Author
|
AI review artifact: independent general reviewer (final round)
|
On JDK 8 a String is never byte-backed, so Latin1JsonReader rejects String input there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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?
A custom
JsonValueCodeccannot get the source text of a JSON value it reads:JsonReader.sliceis protected andposition()is@Internal. Codecs that accept "an object, or the same object as a JSON string" and pass the object to another parser have to copy it token by token into another writer and parse it again. Details in #4229.What does this PR do?
public final String JsonReader.readRawValue(): reads the next complete JSON value and returns its exact source text. It is the read-side counterpart of@JsonRawValue/writeRawValue(String).nulltoken returns the text"null"(a codec that wants Java null checkstryReadNullToken()first). The reader stops right after the value, so the caller consumes the following comma or closing delimiter as usual.skipValue(), so it gets the same checks: string escapes and control characters, UTF-8 validity on the UTF-8 reader, number grammar, andmaxDepthcounted from the current depth. The returned String is a leaf and is not charged to the graph-memory budget; its length is bounded by input already consumed.JsonReadergets a protectedrawText(start, end)hook that defaults toslice. Latin-1 and UTF-16 readers use it as is.Utf8JsonReaderoverrides it to decode the span as UTF-8, because itsslicebuilds a Latin-1 string and is only correct for ASCII token spans (numbers, zone ids).docs/json/custom-codecs.mdwith an object-or-string codec example;docs/json/annotations.mdpoints to it from theJsonRawValueread note.ForyJsonTokenOperationTest.readRawValueKeepsSourceText: all three reader kinds on the same input (object with inner whitespace, escaped and non-ASCII string, number,true,null, empty object, newline between values), CJK and surrogate-pair text on the UTF-16 and UTF-8 readers, the rootreadUtf8token API, invalid literal and empty input.JsonCodecAnnotationTest.rawValueCodec(codegen and interpreted, via the existing factory): a@JsonCodecString property read withreadRawValueand written withwriteRawValue, round trips from Latin-1 String, UTF-16 String and UTF-8 bytes, scalar and array values, malformed values (leading zero, unterminated string, trailing comma), andmaxDepth.slicemakes both tests fail on non-ASCII input.Not included, on purpose: a
byte[]variant for the UTF-8 reader and a read mode for@JsonRawValueproperties. The latter would change what existing models accept; both can follow if maintainers want them.Related issues
JsonReader, which custom codecs already use, and does not depend on it.AI Contribution Checklist
Substantial AI assistance was used in this PR:
yesIf
yes, I included a completed AI Contribution Checklist in this PR description and the requiredAI Usage Disclosure.If
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.I performed a line-by-line self-review of all AI-assisted code changes.
I addressed all AI review comments and repeated the review loop until both ai reviewers reported no further actionable comments.
I attached screenshot evidence or 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.
Verification
JDK 21.0.2, Windows 11 x86_64, base
apache/mainc8bb610:java/:mvn -pl fory-json -am install -DskipTests, thenmvn -pl fory-json test spotless:check checkstyle:checkat fcdae7a: fory-json 1336 tests, 0 failures; spotless and checkstyle clean. The last commit (501542c) only adds test cases;ForyJsonTokenOperationTestandJsonCodecAnnotationTestrerun green there.docs/json: prettier clean for the changed files.ForyJsonTokenOperationTestandJsonCodecAnnotationTest(both codegen modes) pass; with theUtf8JsonReader.rawTextoverride replaced byslice,readRawValueKeepsSourceTextand bothrawValueCodecruns fail on non-ASCII input.Does this PR introduce any user-facing change?
JsonReader.readRawValue(). No existing behaviour changes.Benchmark
No benchmark: no existing read or write path changed.
🤖 Generated with Claude Code