Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Fail to deserialize i128 in internally tagged enum #391

Open
gpollo opened this issue Oct 11, 2023 · 1 comment
Open

Fail to deserialize i128 in internally tagged enum #391

gpollo opened this issue Oct 11, 2023 · 1 comment

Comments

@gpollo
Copy link

gpollo commented Oct 11, 2023

Hi, I'm having trouble de-serializing i128 when it is in a tagged enum. Here's a MWE of the bug. Any ideas why this might happen? Am I doing something wrong? It works when I replace the i128 with a i64.

$ cargo run
Error: i128 is not supported
main.rs
use indoc::indoc;
use anyhow::Result;
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize)]
pub struct IntegerType<T> {
    pub value: T,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "type")]
pub enum Type {
    // replacing i128/u128 by i64/u64 works

    #[serde(rename(deserialize = "signed"))]
    Signed(IntegerType<i128>),
    #[serde(rename(deserialize = "unsigned"))]
    Unsigned(IntegerType<u128>),
}

fn main() -> Result<()> {
    let yaml = indoc! {"
        type: signed
        value: 123
    "};

    let yaml: Type = serde_yaml::from_str(yaml)?;
    println!("{:#?}", yaml);

    Ok(())
}
Cargo.lock
[package]
name = "test-serde-yaml"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
indoc = "2"
serde_yaml = "0.9"

serde = { version = "1", features = ["derive"] }
@gpollo gpollo changed the title Fail to deserialize i128 in this scenario Fail to deserialize i128 in tagged enum Oct 11, 2023
@gpollo gpollo changed the title Fail to deserialize i128 in tagged enum Fail to deserialize i128 in internally tagged enum Oct 11, 2023
@gpollo
Copy link
Author

gpollo commented Oct 11, 2023

Did the same test with serde_json, seems like it's not working there either. Might be a problem with serde itself.

main.rs
use indoc::indoc;
use anyhow::Result;
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize)]
pub struct IntegerType<T> {
    pub value: T,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "type")]
pub enum Type {
    // replacing i128/u128 by i64/u64 works

    #[serde(rename(deserialize = "signed"))]
    Signed(IntegerType<i128>),
    #[serde(rename(deserialize = "unsigned"))]
    Unsigned(IntegerType<u128>),
}

fn main() -> Result<()> {
    let yaml: Result<Type, _> = serde_yaml::from_str(indoc! {"
        type: signed
        value: 123
    "});

    let json: Result<Type, _> = serde_json::from_str(indoc! {"
        {
            \"type\": \"signed\",
            \"value\": 123
        }
    "});
    
    println!("{:#?}", yaml);
    println!("{:#?}", json);

    Ok(())
}
Cargo.lock
[package]
name = "test-serde-yaml"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
indoc = "2"
serde_json = "1.0"
serde_yaml = "0.9"

serde = { version = "1", features = ["derive"] }

EDIT: seems related to serde-rs/serde#2576

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant