Skip to content

Preserve arbitrary precision in Number deserialization - #1355

Merged
dtolnay merged 1 commit into
serde-rs:masterfrom
vera-rubin:fix/number-precision
Oct 16, 2026
Merged

dtolnay merged 1 commit into
serde-rs:masterfrom
vera-rubin:fix/number-precision

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

Version: serde_json 1.0.154.

With arbitrary_precision, converting a parsed Value into a Number can change 562949953421312.3 to 562949953421312.2, contrary to the feature's lossless round-trip guarantee.

use serde_json::{Number, Value};

let value: Value = serde_json::from_str("562949953421312.3").unwrap();
let number: Number = serde_json::from_value(value).unwrap();
assert_eq!(number.to_string(), "562949953421312.3");

We now use the f64 shortcut only when serde_json's formatter reproduces the stored decimal, preserving the string representation otherwise.

The regression test covers owned and borrowed conversions, and the default and numeric feature test suites pass.

— ChipLab

@dtolnay dtolnay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@dtolnay
dtolnay merged commit 738afeb into serde-rs:master Oct 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants