Skip to content

🐛 Emit compact scientific notation for large/small floats in to_json#178

Merged
frenck merged 1 commit into
mainfrom
frenck/to-json-compact-floats
Jul 2, 2026
Merged

🐛 Emit compact scientific notation for large/small floats in to_json#178
frenck merged 1 commit into
mainfrom
frenck/to-json-compact-floats

Conversation

@frenck

@frenck frenck commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Breaking change

None. to_json float values are unchanged and still parse to the same number; only the textual spelling of large/small-magnitude floats changes (e.g. 1e100 was a 101-digit decimal, now 1.0e+100).

Proposed change

to_json rendered floats with Rust's default Display, which never uses exponential notation, so 1e16 became 10000000000000000.0 and 1e100 a 101-digit string. The values were valid JSON but the output ballooned and diverged from stdlib json, orjson, and yamlrocks's own dumps.

write_float now routes through the shared emit_util::canonical_float (the YAML path already uses it), so a large or tiny magnitude emits as 1.0e+16. The mantissa's dot keeps it distinguishable from an integer, non-finite values still project to null, and the exponent form is valid JSON that parses back exactly.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

  • This PR fixes or closes issue: None
  • This PR is related to: None
  • Link to a separate documentation pull request: None

Checklist

  • I have read the AI Policy, and this pull request was not created by an autonomous agent.
  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and uv run pytest passes locally. A pull request cannot be merged unless CI is green.
  • uv run ruff check . and uv run ruff format --check . pass.
  • cargo fmt --check and cargo clippy --all-targets -- -D warnings pass.
  • Round-trip fidelity is preserved: an unmodified document still re-emits byte-for-byte.
  • No commented-out or dead code is left in the pull request.

@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Jul 2, 2026
Copilot AI review requested due to automatic review settings July 2, 2026 22:59
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Jul 2, 2026
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@frenck, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 39 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b4b3e226-a780-42e3-abe1-73490a536444

📥 Commits

Reviewing files that changed from the base of the PR and between 18bee68 and e6fa087.

📒 Files selected for processing (2)
  • src/encode/json.rs
  • tests/core/test_json.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codspeed-hq

codspeed-hq Bot commented Jul 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing frenck/to-json-compact-floats (e6fa087) with main (18bee68)

Open in CodSpeed

@codecov-commenter

codecov-commenter commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.49%. Comparing base (e7132bc) to head (e6fa087).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #178      +/-   ##
==========================================
+ Coverage   92.38%   92.49%   +0.11%     
==========================================
  Files          40       40              
  Lines       11214    11273      +59     
==========================================
+ Hits        10360    10427      +67     
+ Misses        854      846       -8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

to_json rendered floats with Rust's default Display, which never uses
exponential notation, so 1e16 became 10000000000000000.0 and 1e100 a 101-digit
string. The values were correct and valid JSON, but the output ballooned and
diverged from stdlib json, orjson, and yamlrocks's own dumps.

Route write_float through the shared emit_util::canonical_float (the YAML path
already uses it), so a large or tiny magnitude emits as 1.0e+16. The mantissa's
dot keeps it distinguishable from an integer, non-finite values still project
to null above, and the exponent form is valid JSON that parses back exactly.
@frenck frenck force-pushed the frenck/to-json-compact-floats branch from 82a4a31 to e6fa087 Compare July 2, 2026 23:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the to_json float emitter so that large- and small-magnitude floats are rendered in compact scientific notation instead of Rust's default Display, which never uses exponents. Previously 1e16 became 10000000000000000.0 and 1e100 expanded to a 101-digit decimal, diverging from stdlib json, orjson, and yamlrocks's own dumps. The fast JSON path (src/encode/json.rs) now delegates to the shared emit_util::canonical_float helper already used by the YAML emit path.

I verified that canonical_float always produces valid JSON for finite floats (the mantissa always carries a ., and both the scientific and decimal branches emit spec-valid JSON numbers), that non-finite values are still projected to null before reaching the helper, and that float mapping-key stringification (key_string) reuses write_float, so keys stay consistent. The new test exercises boundary (1e16), extreme (5e-324, max f64), and ordinary (0.1) values and confirms exact round-tripping through stdlib json. No documentation examples depend on the old positional spelling.

Changes:

  • Route write_float in the JSON emitter through the shared canonical_float so large/tiny floats emit as e.g. 1.0e+16.
  • Add a parametrized test asserting compact scientific notation and exact stdlib-json round-trip.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/encode/json.rs write_float now delegates to emit_util::canonical_float for compact, valid-JSON float spelling.
tests/core/test_json.py New parametrized test verifying compact scientific notation and exact round-trip through stdlib json.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings July 2, 2026 23:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@frenck frenck merged commit f82cd76 into main Jul 2, 2026
74 checks passed
@frenck frenck deleted the frenck/to-json-compact-floats branch July 2, 2026 23:43
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants