Does serde_json handle f64 in a special way?

Hi, experts.

serde_json parses the 1.23456e30 as f64 which is different from let x = 1.23456e30.
Why?

Here are my depedencies:

[dependencies]
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"

Here is the code:

use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct f64s {
    values: Vec<f64>
}

    fn test_serde_json_f64() {
        let bytes = 1.23456e30_f64.to_le_bytes();
        println!("expected:   {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", 
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
            bytes[5], bytes[6], bytes[7]);
    
         let data = r#"
        {
            "values": [1.23456e30]
        }"#;
        let v: f64s = serde_json::from_str(data).unwrap();
        let bytes = v.values[0].to_le_bytes();
        println!("serde_json: {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", 
            bytes[0], bytes[1], bytes[2], bytes[3],
            bytes[4], bytes[5], bytes[6], bytes[7]);
    }

Here is the output:

expected:   d55eae31282a2f46
serde_json: d65eae31282a2f46

Many thanks in advance.

I can't reproduce this on the playground, which gives me

expected:   d55eae31282a2f46
serde_json: d55eae31282a2f46

as output. What platform are you running locally?

Thank you for your kind reply. It is Windows 10 x64 with msvc. vs2022 installed.

This might be the same problem you are having:

maybe try enabling the float_roundtrip feature on serde_json and see if it deserialises correctly?

Edit: this would explain why I can't reproduce it on the playground, where the float_roundtrip feature is enabled.

3 Likes

Thank you! That does fix the issue.

serde_json = { version = "1.0.140", features = ["float_roundtrip"] }
2 Likes