Json: u128 is not supported with `#[serde(flatten)]`

Created on 21 Feb 2020  路  4Comments  路  Source: serde-rs/json

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.

Most helpful comment

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

All 4 comments

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?

I meet the same problem

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

atenjin picture atenjin  路  3Comments

mnacamura picture mnacamura  路  6Comments

elliottslaughter picture elliottslaughter  路  5Comments

kazimuth picture kazimuth  路  3Comments

allan-simon picture allan-simon  路  6Comments