feat(json): encode byte arrays as base64 JSON strings by default - #4012
Open
ingokegel wants to merge 2 commits into
Open
feat(json): encode byte arrays as base64 JSON strings by default#4012ingokegel wants to merge 2 commits into
ingokegel wants to merge 2 commits into
Conversation
JSON has no binary type, and the ecosystem standard for byte arrays in JSON is a base64 string (RFC 7493, the protobuf JSON mapping, Jackson, Gson, Moshi and kotlinx.serialization). The previous default wrote byte[] as a JSON array of decimal numbers, which is several times larger on the wire and much slower to write and parse. The @JsonBase64 annotation already provided the base64 encoding as an opt-in. This change makes it the default for byte[] properties, so the annotation is no longer needed for that. Base64 reads now reserve the decoded array in the graph memory budget, like other array reads. BREAKING CHANGE: byte[] values are written as base64 JSON strings instead of decimal number arrays. JSON written with earlier versions cannot be read back with the default codec.
Base64 decoding did a branch-heavy validation scan and then a second pass that re-read every character with per-character escape handling and a four-way branch per digit. On binary-heavy payloads this dominated the deserialization time. The validation now runs as a single table-driven scan, and the decode pass uses the same digit table without re-checking escapes. Bodies with escaped characters fall back to the previous validating two-pass path.
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?
Closes #4011.
JSON has no binary type. The ecosystem standard for byte arrays in JSON is a base64 string (see RFC 7493, the protobuf JSON mapping, Jackson, Gson, Moshi, kotlinx.serialization). Fory JSON currently writes byte[] as a JSON array of decimal numbers, which is about 2.1x larger on the wire and about 4.5x slower to write and read on binary-heavy payloads.
What does this PR do?
Related issues
Closes #4011
AI Contribution Checklist
yes/noyes, 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.Does this PR introduce any user-facing change?
Benchmark
Round trip on a realistic payload with ~5 KB byte[] thumbnails, 2M iterations, JDK 25, Linux x86_64, project: https://github.com/ej-technologies/serialization-comparison