🐛 Emit compact scientific notation for large/small floats in to_json#178
Conversation
|
Warning Review limit reached
Next review available in: 39 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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.
82a4a31 to
e6fa087
Compare
There was a problem hiding this comment.
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_floatin the JSON emitter through the sharedcanonical_floatso large/tiny floats emit as e.g.1.0e+16. - Add a parametrized test asserting compact scientific notation and exact stdlib-
jsonround-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.
Breaking change
None.
to_jsonfloat values are unchanged and still parse to the same number; only the textual spelling of large/small-magnitude floats changes (e.g.1e100was a 101-digit decimal, now1.0e+100).Proposed change
to_jsonrendered floats with Rust's defaultDisplay, which never uses exponential notation, so1e16became10000000000000000.0and1e100a 101-digit string. The values were valid JSON but the output ballooned and diverged from stdlibjson, orjson, and yamlrocks's owndumps.write_floatnow routes through the sharedemit_util::canonical_float(the YAML path already uses it), so a large or tiny magnitude emits as1.0e+16. The mantissa's dot keeps it distinguishable from an integer, non-finite values still project tonull, and the exponent form is valid JSON that parses back exactly.Type of change
Additional information
Checklist
uv run pytestpasses locally. A pull request cannot be merged unless CI is green.uv run ruff check .anduv run ruff format --check .pass.cargo fmt --checkandcargo clippy --all-targets -- -D warningspass.