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

Bug: f64 within flattened HashMap throws error on deserialization #1157

Open
smessmer opened this issue Jul 25, 2024 · 1 comment
Open

Bug: f64 within flattened HashMap throws error on deserialization #1157

smessmer opened this issue Jul 25, 2024 · 1 comment

Comments

@smessmer
Copy link

smessmer commented Jul 25, 2024

serde = { version = "^1.0.204", features = ["derive"] }
serde_json = { version = "^1.0.120", features = ["arbitrary_precision"] }

Code:

#[derive(Serialize, Deserialize)]
struct MainStruct {
    #[serde(flatten)]
    key_value_pairs: HashMap<String, f64>,
}

let obj = MainStruct {
    key_value_pairs: [("key".to_string(), 1.0f64)].into_iter().collect(),
};

let serialized = serde_json::to_string(&obj).unwrap();
println!("{:?}", serialized);
let _parsed: MainStruct = serde_json::from_str(&serialized).unwrap();

This prints the correctly serialized string

{"key":1.0}

But when it tries to deserialize it again, it panics with:

called `Result::unwrap()` on an `Err` value: Error("invalid type: map, expected f64", line: 1, column: 11)
@purplesyringa
Copy link
Contributor

This seems to be a variation of #959.

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

2 participants