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

u128 is not supported with #[serde(flatten)] #625

Open
nlordell opened this issue Feb 21, 2020 · 4 comments
Open

u128 is not supported with #[serde(flatten)] #625

nlordell opened this issue Feb 21, 2020 · 4 comments

Comments

@nlordell
Copy link
Contributor

nlordell commented Feb 21, 2020

It looks like u128 is not supported with #[serde(flatten)] when deserializing JSON. The following snippet shows the problem.

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Foo {
    a: u128,
}

#[derive(Debug, Deserialize)]
struct Bar {
    foo: Foo,
}

#[derive(Debug, Deserialize)]
struct Baz {
    #[serde(flatten)]
    foo: Foo,
}

fn main() {
    println!("{:?}", serde_json::from_str::<Foo>(r#"{"a":340282366920938463463374607431768211455}"#));
    println!("{:?}", serde_json::from_str::<Bar>(r#"{"foo":{"a":340282366920938463463374607431768211455}}"#));
    println!("{:?}", serde_json::from_str::<Baz>(r#"{"a":340282366920938463463374607431768211455}"#));
}

Outputs:

Ok(Foo { a: 340282366920938463463374607431768211455 })
Ok(Bar { foo: Foo { a: 340282366920938463463374607431768211455 } })
Err(Error("u128 is not supported", line: 1, column: 45))

This might be a serde_derive issue though, and not related to serde_json.

@nlordell
Copy link
Contributor Author

Looks like this is a serde issue as for flattened structs, it buffers the visited values in a serde::private::de::Content first which does not support {i,u}128s.

Should I move the issue over?

@yaanhyy
Copy link

yaanhyy commented Apr 1, 2020

I meet the same problem

@rudolfs
Copy link

rudolfs commented Jun 8, 2020

Still happening in serde_json "1.0.53" (in combination with serde "1.0.111").

use serde_json::json;

fn main() {
    let number: u128 = 0;
    let obj = json!({
        "number": number,
    });

    println!("{}", obj.to_string());
}
Error("u128 is not supported", line: 0, column: 0)', src/main.rs:5:15

@soulmachine
Copy link

I have a i128 field and got the same issue today, error message is i128 is not supported

Hywan added a commit to Hywan/matrix-rust-sdk that referenced this issue Jul 7, 2022
First, u128 has a bug in `serde`,
cf. serde-rs/json#625.

Second, we don't need to represent the timeout as a u128, it's clearly
too large. This patch tries to convert it to u64. It should never
fail, but we propagate the error anyway.
Slixe added a commit to xelis-project/xelis-blockchain that referenced this issue Feb 13, 2023
Slixe added a commit to xelis-project/xelis-blockchain that referenced this issue Apr 17, 2023
jplatte pushed a commit to matrix-org/matrix-rust-sdk-crypto-nodejs that referenced this issue Jul 12, 2023
First, u128 has a bug in `serde`,
cf. serde-rs/json#625.

Second, we don't need to represent the timeout as a u128, it's clearly
too large. This patch tries to convert it to u64. It should never
fail, but we propagate the error anyway.
Yael-Starkware added a commit to starkware-libs/papyrus that referenced this issue Oct 29, 2023
Yael-Starkware added a commit to starkware-libs/papyrus that referenced this issue Oct 29, 2023
W95Psp pushed a commit to hacspec/hax that referenced this issue Jun 30, 2024
W95Psp pushed a commit to hacspec/hax that referenced this issue Jul 1, 2024
Pantom-007 added a commit to Pantom-007/Xelis-Blockchain that referenced this issue Sep 17, 2024
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