json icon indicating copy to clipboard operation
json copied to clipboard

f64 in adjacently tagged enums fails to deserialize when arbitrary_precision feature is enabled

Open sevazhidkov opened this issue 11 months ago • 3 comments

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.0 with 1
  • Removing #[serde(tag = "kind", content = "data")] and changing the JSON structure accordingly
  • Removing arbitrary_precision feature
  • Deserializing to serde::Value first and, then, deserializing serde::Value to Enum
  • Place kind before data in JSON
seva@X serde-issue % rustc -V
rustc 1.65.0 (897e37553 2022-11-02)

Also reproduces in RustExplorer.

sevazhidkov avatar Jul 27 '23 21:07 sevazhidkov