json icon indicating copy to clipboard operation
json copied to clipboard

The`#[serde(flatten)]` syntax is not supported with the feature `arbitrary_precision`

Open koushiro opened this issue 4 years ago • 4 comments

example:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Inner {
    inner: u32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Outer {
    out: u32,
    #[serde(flatten)]
    inner: Inner,
}

fn main() {
    let out = Outer {
        out: 1,
        inner: Inner {
            inner: 2,
        }
    };
    let ser = serde_json::to_string(&out).unwrap();
    println!("Out: {}", ser);
    let de: Outer = serde_json::from_str(&ser).unwrap();
    println!("Out: {:?}", de);
}

with the feature arbitrary_precision:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["arbitrary_precision"] }

result:

    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/json-demo`
Out: {"out":1,"inner":2}
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: map, expected u32", line: 1, column: 19)', src/main.rs:24:48
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

without the feature arbitrary_precision:

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

result:

    Finished dev [unoptimized + debuginfo] target(s) in 2.39s
     Running `target/debug/json-demo`
Out: {"out":1,"inner":2}
Out: Outer { out: 1, inner: Inner { inner: 2 } }

koushiro avatar Oct 14 '20 13:10 koushiro

@dtolnay Can you give me some tips on how to fix this issue?

koushiro avatar Oct 14 '20 13:10 koushiro

It does not work with f64 either. It fails even without arbitrary_precision. Suppose it is a bug of serde(flatten)?

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct Inner {
        pub rotation: [f64; 3],
        pub translation: [f64; 3],
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct Outer {
        #[serde(flatten)]
        inner: Inner,
    }

    #[test]
    fn wtf() -> Result<()> {
        let text = r#"{
  "rotation": [
    0.7547604535512583,
    -0.12463161307955047,
    0.10464618142566347,
    0.6354941348976522
  ],
  "translation": [
    -0.07889085071772557,
    -0.05290491930288589,
    -0.04170572461992302
  ]
}
"#;
        let _: Outer = serde_json::from_str(&text)?;

        Ok(())
    }
}

jerry73204 avatar May 13 '21 19:05 jerry73204

I just ran into this. I have two #[serde(flatten)] fields that were working and it took me a couple hours to figure out that it was the addition of the arbitrary precision feature by another dependency that was causing my tests to fail.

tylerhawkes avatar Sep 10 '21 17:09 tylerhawkes

I just ran into this. I have two #[serde(flatten)] fields that were working and it took me a couple hours to figure out that it was the addition of the arbitrary precision feature by another dependency that was causing my tests to fail.

Yeah also just ran into this in my project. It can be a pretty sinister bug to track down. I'm honestly surprised this issue is still open considering how code breaking it can be. Took me and a coworker like 5 hours combined to track it down.

0xForerunner avatar May 12 '23 00:05 0xForerunner