Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to deserialize tagged enum when inner struct has a number with trailing zeros #2903

Open
codingskynet opened this issue Mar 7, 2025 · 0 comments

Comments

@codingskynet
Copy link

#[derive(Debug, serde::Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum Data {
    #[serde(alias = "a")]
    A(Info),
}

#[derive(Debug, serde::Deserialize)]
struct Info {
    timestamp: f64,
}

fn main() {
    let data = r#"{"type": "a", "data": {"timestamp": 1.10}}"#;

    let value: serde_json::Value = serde_json::from_str(data).unwrap();
    println!("{:?}", value);

    let data: Data = serde_json::from_value(value).unwrap();
    println!("{:?}", data);
}

serde: 1.0.218, features = ["std", "derive"]
serde_json: 1.0.140, features = ["std", "arbitrary_precision"]

If arbitrary_precision is off, succeed to deserialize by removing trailing zeros:

Object {"data": Object {"timestamp": Number(1.1)}, "type": String("a")}
A(Info { timestamp: 1.1 })

But, if arbitrary_precision is on, fail to deserialize due to trailing zeros:

Object {"data": Object {"timestamp": Number(1.10)}, "type": String("a")}
thread 'main' panicked at src/main.rs:19:52:
called `Result::unwrap()` on an `Err` value: Error("invalid type: map, expected f64", line: 0, column: 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant