You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have JSON data generated by JavaScript (node). Thus, all containing float values should be valid IEEE 754 double precision literals. However, a parse + write roundtrip through serde_json leads to modified data.
#128 has been closed with the info that there will be a follow-up issue to track, but I couldn't find it, and the conversation is locked. I'm wondering what is the current status of float roundtrips. I'm noticing surprising effects:
// To verify that 117.63331139400017 is a literal that can be fully represented.let x_val = 117.63331139400017;let x_str = x_val.to_string();println!("{} == {}", x_val, x_str);// serialization => deserialization roundtriplet x_parsed:f64 = serde_json::from_str(&json!(x_val).to_string()).unwrap();println!("{} == {}", x_val, x_parsed);// It looks like it is actually the deserialization:println!("117.63331139400017 == {:?}", serde_json::from_str::<f64>("117.63331139400017").unwrap());
If you need parse + write roundtrip to preserve full precision, consider enabling the arbitrary_precision feature and using serde_json::Number instead of f64.
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
I noticed that this doesn't work in general though, in particular the example I gave above also fails with arbitrary_precision feature flag enabled. The reason seems to be that parsing behaves differently when going straight to f64 compared to taking a detour through Value, which could lead to tricky bugs:
I have JSON data generated by JavaScript (node). Thus, all containing float values should be valid IEEE 754 double precision literals. However, a parse + write roundtrip through serde_json leads to modified data.
#128 has been closed with the info that there will be a follow-up issue to track, but I couldn't find it, and the conversation is locked. I'm wondering what is the current status of float roundtrips. I'm noticing surprising effects:
Output:
Tested with latest version (1.0.44) of serde_json.
The text was updated successfully, but these errors were encountered: