json
json copied to clipboard
f64 in adjacently tagged enums fails to deserialize when arbitrary_precision feature is enabled
The crate fails to deserialize this JSON, when arbitrary_precision feature is enabled:
{
"data": {
"field": 1.0
},
"kind": "Variant"
}
A reproducible snippet that panics at the serde_json::from_str line:
/*
[dependencies]
serde = {version = "1.0.177", features = ["derive"] }
serde_json = {version = "1.0.104", features = ["arbitrary_precision"]}
*/
use serde::{Deserialize};
#[derive(Deserialize, Debug)]
#[serde(tag = "kind", content = "data")]
pub enum Enum {
Variant {
field: f64
}
}
fn main() {
let string = r#"{
"data": {
"field": 1.0
},
"kind": "Variant"
}"#;
// thread 'main' panicked at 'works: Error("invalid type: map, expected f64", line: 6, column: 5)', src/main.rs:25:59
let deserialized: Enum = serde_json::from_str(string).expect("works");
println!("{:?}", deserialized);
}
Any of the following actions make the panic go away:
- Replacing
1.0with1 - Removing
#[serde(tag = "kind", content = "data")]and changing the JSON structure accordingly - Removing
arbitrary_precisionfeature - Deserializing to
serde::Valuefirst and, then, deserializingserde::ValuetoEnum - Place
kindbeforedatain JSON
seva@X serde-issue % rustc -V
rustc 1.65.0 (897e37553 2022-11-02)
Also reproduces in RustExplorer.
Oh, it seems like a duplicate of #959. However, should it be closed if it's not fixed yet? Might be worth it to track it as a known bug. Anyway, feel free to close this new issue, sorry for bringing it again.
Can confirm this error still happens with the latest release.
Is there an older version not affected by this?