Skip to content

Read JSON numbers as Decimal so long-precision values aren't changed - #69

Merged
plamen-neykov merged 2 commits into
mainfrom
fix-number-precision-when-reading
Nov 10, 2026
Merged

plamen-neykov merged 2 commits into
mainfrom
fix-number-precision-when-reading

Conversation

@dschwartznyc

@dschwartznyc dschwartznyc commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes #60.

Problem

rune_deserialize parsed JSON with plain json.loads, and model_validate_json with pydantic's parser. Both turn an unquoted number with a fraction or exponent into a binary float before it becomes a Decimal, so values with more than about 15 to 17 significant digits changed with no error, and a trailing zero was lost:

JSON number Read before Read now
12345678901234567.89 12345678901234568 12345678901234567.89
0.1000000000000000000001 0.1 0.1000000000000000000001
50000.10 50000.1 50000.10

Java writes numbers unquoted, so this affected JSON written by Java. Quoted numbers and whole numbers were already read exactly.

Change

  • rune_deserialize parses with json.loads(rune_data, parse_float=Decimal).
  • BaseDataClass.model_validate_json is overridden to parse the same way, then call model_validate. Pydantic 2.14 has no setting to read JSON floats as Decimal. With strict=True, pydantic's own parsing is kept, so strict validation behaves as before; validating parsed values strictly would reject JSON forms that strict mode accepts today, such as an enum's value or a UUID string.
  • New test/test_number_precision.py, 25 tests, using the classes rune-python-generator produces for number, list, [metadata scheme], int, date and enum attributes.

What this closes in #60

  • Closes: the reading problem described in the comment on Python and Java write Rune number values differently #60 from 2026-11-09, which superseded the original recommendation, for both rune_deserialize and model_validate_json.
  • Not changed, as decided: how numbers are written. Python keeps writing number values as strings ("50000"). Java reads quoted numbers exactly, and RFC 7493 recommends strings for long precision, so the writer change first proposed in Python and Java write Rune number values differently #60 isn't needed.
  • Strict mode: model_validate_json(..., strict=True) keeps pydantic's parsing and still rounds long numbers. On BaseDataClass, strict mode also rejects dates, times and numbers in their JSON forms, on main as well.

With the writer change dropped, nothing else in #60 remains, so the issue can be closed when this merges.

Related

PR #62 also makes the parse_float=Decimal change in rune_deserialize, together with the writer change that #60 no longer needs.

Testing

  • test/test_number_precision.py: 25 passed. On main, 19 of the 25 fail.
  • Full suite in a clean environment (pip install .[dev] plus pytest, pytest-cov and pytest-mock, as in run-tests.yml): 379 passed, 20 skipped. The skips are the tests that need the generated serialization package or a CDM package.

@github-actions

Copy link
Copy Markdown

Test Results

399 tests  +25   379 ✅ +25   1s ⏱️ ±0s
  1 suites ± 0    20 💤 ± 0 
  1 files   ± 0     0 ❌ ± 0 

Results for commit 680fcb0. ± Comparison against base commit 8b7cdf8.

@plamen-neykov plamen-neykov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

@plamen-neykov
plamen-neykov merged commit f305cf9 into main Nov 10, 2026
6 checks passed
@dschwartznyc
dschwartznyc deleted the fix-number-precision-when-reading branch November 10, 2026 20:15
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.

Python and Java write Rune number values differently

3 participants