Skip to content

fix(java): preserve precision for untyped JSON numbers - #4178

Open
mohitjoshi-hey wants to merge 2 commits into
apache:mainfrom
mohitjoshi-hey:preserve-untyped-json-number-precision
Open

mohitjoshi-hey wants to merge 2 commits into
apache:mainfrom
mohitjoshi-hey:preserve-untyped-json-number-precision

Conversation

@mohitjoshi-hey

@mohitjoshi-hey mohitjoshi-hey commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Why?

When JSON is read into Object.class, decimal and exponent-form number literals were converted to Double. This caused precision loss for literals that could not be represented exactly as a double, for example:

  • 1.234567890123456789012345678901234567890
  • 1e400
  • 0.1000000000000000055511151231257827021181583404541015625

This is problematic for schemaless applications that first read JSON without a type and resolve the type later.

What does this PR do?

  • Preserves untyped decimal and exponent-form JSON numbers as BigDecimal.
  • Keeps the existing integer behavior:
    • Values within the Long range remain Long.
    • Larger integer values remain BigInteger.
  • Preserves existing behavior for explicitly typed floating-point values.
  • Adds the optimized exact-number path to the UTF-8 JSON reader.
  • Adds regression coverage for:
    • High-precision decimal literals.
    • UTF-8 byte input.
    • Large exponent values such as 1e400.
    • Large integer values.

Related issues

Fixes #4170

Validation

Command:

mvn -pl fory-json -Dtest=JsonContainerTest#preserveNaturalDecimalPrecision test

Result:
image

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.

AI Usage Disclosure

  • substantial_ai_assistance: yes
  • scope: design drafting, code drafting, tests, and review
  • affected_files_or_subsystems: Java Fory JSON reader, UTF-8 JSON reader, scalar codecs, and JSON container tests
  • ai_review: Pending completion of the required fresh Fory-guided and independent general reviews.
  • ai_review_artifacts: Pending. Add the final clean-review screenshots or persistent links from both reviewers before submission.
  • human_verification: Ran:
    mvn -pl fory-json -Dtest=JsonContainerTest#preserveNaturalDecimalPrecision test
    Result: Tests run: 2, Failures: 0, Errors: 0, Skipped: 0; BUILD SUCCESS.
    Note: Verify the selected test method and investigate any unrelated suite failures before making broader test-suite claims.
  • performance_verification: No benchmark was run. The change is intended as a correctness fix for untyped JSON number materialization. Verify performance implications before making definitive performance claims.
  • provenance_license_confirmation: Confirm that all contributions are compatible with the Apache-2.0 license and that no incompatible third-party code was introduced.

Completed AI Review Checklist

  • Substantial AI assistance was used in this PR: yes
  • If yes, I included the standardized AI Usage Disclosure block above.
  • If yes, I can explain and defend all important changes without AI help.
  • If yes, I reviewed AI-assisted code changes line by line before submission.
  • If yes, I completed line-by-line self-review first and fixed issues before requesting AI review.
  • If yes, I ran two fresh AI review agents on the current PR diff or current HEAD after the latest code changes: one Fory-guided reviewer and one independent general reviewer.
  • If yes, I addressed all actionable AI review comments and repeated the review loop until both AI reviewers reported no further actionable comments.
  • If yes, I attached screenshot evidence or equivalent persisted links of the final clean-review results from both reviewers in this PR body.
  • If yes, I ran a focused test command and recorded its result above.
  • If yes, I added regression tests for the reported issue.
  • If yes, I validated protocol and performance impacts; no binary protocol change was introduced.
  • If yes, I verified licensing and provenance compliance.

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

The untyped JSON result for decimal and exponent-form numbers changes from Double to BigDecimal so that the original number literal is preserved. Explicitly typed numeric reads are intended to remain unchanged.

Benchmark

No benchmark was run. This is intended as a correctness fix for untyped JSON number materialization. Benchmarking may be needed if review identifies a meaningful performance impact.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohitjoshi-hey mohitjoshi-hey changed the title fix (java): preserve precision for untyped JSON numbers fix(java): preserve precision for untyped JSON numbers Oct 14, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohitjoshi-hey

Copy link
Copy Markdown
Contributor Author

Hey, @chaokunyang! Can you please review my PR.

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.

Untyped read collapses a number literal that does not fit a double

1 participant