-
Notifications
You must be signed in to change notification settings - Fork 563
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
Arbitrary precision numbers don't work with serde(tag = ..)
and serde(flatten)
#505
Comments
It also fails for
|
Same for use serde_derive::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
struct Outer {
#[serde(flatten)]
inner: Inner,
}
#[derive(Debug, Serialize, Deserialize)]
struct Inner {
value: usize,
}
fn main() {
let v = Outer {
inner: Inner {
value: 10,
}
};
let json = serde_json::to_string(&v).unwrap();
let v2: Outer = serde_json::from_str(&json).unwrap();
println!("{:#?}", v2);
} Output:
Cargo.toml: [package]
name = "ttt"
version = "0.1.0"
edition = "2018"
[dependencies]
serde_json = { version = "1.0", features = ["arbitrary_precision"]}
serde = "1.0"
serde_derive = "1.0" |
serde(tag = ..)
serde(tag = ..)
and serde(flatten)
Well, the workaround is as simple as deserializing to #[macro_use]
extern crate serde_derive;
#[derive(Deserialize)]
struct Data {
value: i32,
}
#[derive(Deserialize)]
#[serde(tag = "type")]
enum Wrapper {
Data(Data),
}
fn main() {
let json = r#"{"type":"Data","value":123}"#;
// Okay
let _data1: Data = serde_json::from_str(json).unwrap();
// Fails!
//let _data2: Wrapper = serde_json::from_str(json).unwrap();
// Works!
let data2: serde_json::Value = serde_json::from_str(json).unwrap();
let _data2: Wrapper = serde_json::from_value(data2).unwrap();
} Not great, but okay for our purposes. I think, what happens is serde JSON parser is confused when it is asked to deserialize into this |
Demonstration of what happens. The following code: #[macro_use]
extern crate serde_derive;
#[derive(Debug, Deserialize)]
struct SneakyData {
value: std::collections::HashMap<String, String>,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
enum SneakyWrapper {
Data(SneakyData),
}
fn main() {
let json = r#"{"type":"Data","value":123}"#;
// Sneaky!
let data: SneakyWrapper = serde_json::from_str(json).unwrap();
println!("{:#?}", data);
} prints:
|
Thanks for the workaround I also ran in to this issue. |
When having a u128 type in the struct i ran into a combination of the previously reported and See here: marcelbuesing@dab262c#diff-9a86b31b12f3b48a02818b844654ab0cR35
|
When we have a struct that uses `#[serde(flatten)]` and the feature `arbitrary_precision` is enabled, the deserialization. The problem was that the arbitrary number was forwarded as a "map" to the visitor and in a later step this map failed as a number was expected. Fixes: serde-rs#505
When we have a struct that uses `#[serde(flatten)]` and the feature `arbitrary_precision` is enabled, the deserialization. The problem was that the arbitrary number was forwarded as a "map" to the visitor and in a later step this map failed as a number was expected. Fixes: serde-rs#505
Also hitting this issue. Any estimate on a possible fix? |
JSON serialization/deserialization of `i128`/`u128` can only be supported when the `arbitrary_precision` feature enabled. But `arbitrary_precision` numbers don't work with `serde(tag = ..)` and `serde(flatten)` syntax (serde-rs/json#505), as a result, the deserialization of structures in `jsonrpc-core` will be error. Fortunately, the crate `jsonrpc-core` provides a workround(paritytech/jsonrpc#521). Although this workround will cause more extra serialization/deserialization work, but I think it's worth it. Signed-off-by: koushiro <[email protected]>
While using the esc client on a different project that had added the tracing bunyan logger I ran into serde_json [issue 505](serde-rs/json#505). This commit adds a work around to the job GET code which is the only place I've encountered any trouble so far.
Closing as a duplicate of serde-rs/serde#1183. This is not an issue with arbitrary precision, it is primarily an issue with the |
The code below fails if
arbitrary_precision
feature is turned on (works fine if it is turned off):Error backtrace:
Cargo.toml:
The text was updated successfully, but these errors were encountered: