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

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

Open
atenjin opened this issue Aug 19, 2019 · 3 comments

Comments

@atenjin
Copy link

atenjin commented Aug 19, 2019

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!

insipx added a commit to paritytech/substrate-archive that referenced this issue Nov 22, 2019
When deserialializing into untagged enum, serde_json will fail
serde-rs/json#559
@mike-wilson-hudson
Copy link

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 added a commit to paritytech/substrate that referenced this issue Nov 13, 2020
In polkadot:

---- call_function_actually_work stdout ----
thread 'call_function_actually_work' panicked at 'called `Result::unwrap()` on an `Err` value: RpcTransactionError { code: -32700, message: "Parse error", data: None }', node/test/service/tests/call-function.rs:29:60

Issues related:

serde-rs/json#559
serde-rs/serde#773
@cecton
Copy link

cecton commented Nov 13, 2020

There is a trick to circumvent this issue:

		let val = serde_json::from_str::<serde_json::Value>(input)?;
		T::deserialize(val)

@yagehu
Copy link

yagehu commented Feb 26, 2021

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.

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

4 participants