serde-rs / json

Strongly typed JSON library for Rust
Apache License 2.0
4.92k stars 562 forks source link

Deserialize failed when open feature "arbitrary_precision" under `#[serde(untagged)]` #559

Open atenjin opened 5 years ago

atenjin commented 5 years ago

Hi: I need some features for u128 support, thus we open "arbitrary_precision" feature. I already get that under "arbitrary_precision", all number is serialize/deserialize in string, but I find that the deserialize would be failed under #[serde(untagged)] struct. like this: serde_json version: 1.0.40

 #[derive(Serialize, Deserialize, Debug)]
    #[serde(untagged)]
    enum Data {
        Integer(u128),
        Pair(String, String),
    }

    let d: Data = serde_json::from_str("1").unwrap();
    println!("{:?}", d);

this code would panic:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("data did not match any variant of untagged enum Data", line: 0, column: 0)', src/libcore/result.rs:999:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:59
             at src/libstd/panicking.rs:197
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:211
   4: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:474
   5: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:381
   6: rust_begin_unwind
             at src/libstd/panicking.rs:308
   7: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
   8: core::result::unwrap_failed
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/macros.rs:18
   9: core::result::Result<T,E>::unwrap
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/result.rs:800
  10: tmp::main
             at src/main.rs:39
  11: std::rt::lang_start::{{closure}}
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/rt.rs:64
  12: std::panicking::try::do_call
             at src/libstd/rt.rs:49
             at src/libstd/panicking.rs:293
  13: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:85
  14: std::rt::lang_start_internal
             at src/libstd/panicking.rs:272
             at src/libstd/panic.rs:394
             at src/libstd/rt.rs:48
  15: std::rt::lang_start
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/rt.rs:64
  16: main
  17: __libc_start_main
  18: _start

I think there may be some bug in serde drive for feature "arbitrary_precision". we must use u128 in our projects, thus this is important for us. thank you very much!

mike-wilson-hudson commented 4 years ago

Here's another example that does not include a u128. It succeeds on playground, but fails if you have arbitrary_precision enabled:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=24e3a50680cdbe4db5ad1fa49161e3c5

cecton commented 4 years ago

There is a trick to circumvent this issue:

        let val = serde_json::from_str::<serde_json::Value>(input)?;
        T::deserialize(val)
yagehu commented 3 years ago

I just spent 2 hours tearing my hairs out. My project worked fine with serde_json without arbitrary_precision. Then I added tracing-bunyan-formatter which enables this feature to my project and all primitive deserialization inside untagged enums failed.

Looks like tracing-bunyan-formatter already has an issue for this.

jirkakad1994 commented 1 week ago

Hi: I need some features for u128 support, thus we open "arbitrary_precision" feature. I already get that under "arbitrary_precision", all number is serialize/deserialize in string, but I find that the deserialize would be failed under #[serde(untagged)] struct. like this: serde_json version: 1.0.40

 #[derive(Serialize, Deserialize, Debug)]
    #[serde(untagged)]
    enum Data {
        Integer(u128),
        Pair(String, String),
    }

    let d: Data = serde_json::from_str("1").unwrap();
    println!("{:?}", d);

this code would panic:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("data did not match any variant of untagged enum Data", line: 0, column: 0)', src/libcore/result.rs:999:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:59
             at src/libstd/panicking.rs:197
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:211
   4: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:474
   5: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:381
   6: rust_begin_unwind
             at src/libstd/panicking.rs:308
   7: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
   8: core::result::unwrap_failed
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/macros.rs:18
   9: core::result::Result<T,E>::unwrap
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/result.rs:800
  10: tmp::main
             at src/main.rs:39
  11: std::rt::lang_start::{{closure}}
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/rt.rs:64
  12: std::panicking::try::do_call
             at src/libstd/rt.rs:49
             at src/libstd/panicking.rs:293
  13: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:85
  14: std::rt::lang_start_internal
             at src/libstd/panicking.rs:272
             at src/libstd/panic.rs:394
             at src/libstd/rt.rs:48
  15: std::rt::lang_start
             at /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libstd/rt.rs:64
  16: main
  17: __libc_start_main
  18: _start

I think there may be some bug in serde drive for feature "arbitrary_precision". we must use u128 in our projects, thus this is important for us. thank you very much!