Struct with a u128 field can't be deserialized when inside an untagged enum

use serde::Deserialize;

struct U128 {
    number: u128,

struct U64 {
    number: u64,

enum Container<T> {

fn main() {
    let json_str = "{ \"number\": 500 }";

    // The following line works
    let _: Container<U64> = serde_json::from_str(json_str).unwrap();

    // Also works
    let _: U128 = serde_json::from_str(json_str).unwrap();

    // Panics!
    let _: Container<U128> = serde_json::from_str(json_str).unwrap();

Why does the deserialization of the struct with u128 field fail when put inside the untagged enum? Is this a bug?

Yes, that is a bug in serde. untagged enums do not support 128 bit integers · Issue #1717 · serde-rs/serde · GitHub
While deserializing an untagged enum, data is buffered. The buffer is not capable of holding i128/u128 values [src]. There is no way around that, except patching serde yourself. Maybe in some future, the internal buffering can be reworked to fix many bugs resulting from it (serde#1183).

1 Like

Thanks, I was confused by this issue a lot while working on a library project.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.