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

Untagged enum does not play nice with u128 #1682

Open
WiSaGaN opened this issue Nov 26, 2019 · 2 comments · May be fixed by #2781
Open

Untagged enum does not play nice with u128 #1682

WiSaGaN opened this issue Nov 26, 2019 · 2 comments · May be fixed by #2781

Comments

@WiSaGaN
Copy link
Contributor

WiSaGaN commented Nov 26, 2019

I would expect both tests pass, but instead, only test64 passed, while test128 failed with

data did not match any variant of untagged enum E128

Maybe related to serde-rs/json#559

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[derive(Debug, PartialEq)]
pub struct S64 {
    pub f: u64,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
#[derive(Debug, PartialEq)]
pub enum E64 {
    S(S64),
}

#[derive(Deserialize, Serialize)]
#[derive(Debug, PartialEq)]
pub struct S128 {
    pub f: u128,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
#[derive(Debug, PartialEq)]
pub enum E128 {
    S(S128),
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test64() {
        let incoming_str = r#"{"f":1}"#;
        let expected_incoming = E64::S(S64 { f: 1 });
        let incoming = serde_json::from_str(&incoming_str).unwrap();
        assert_eq!(expected_incoming, incoming);
    }

    #[test]
    fn test128() {
        let incoming_str = r#"{"f":1}"#;
        let expected_incoming = E128::S(S128 { f: 1 });
        let incoming = serde_json::from_str(&incoming_str).unwrap();
        assert_eq!(expected_incoming, incoming);
    }
}

playground

@WiSaGaN
Copy link
Contributor Author

WiSaGaN commented Nov 26, 2019

Use serde_test instead of serde_json, failed with:

tokens failed to deserialize: data did not match any variant of untagged enum E128

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[derive(Debug, PartialEq)]
pub struct S64 {
    pub f: u64,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
#[derive(Debug, PartialEq)]
pub enum E64 {
    S(S64),
}

#[derive(Deserialize, Serialize)]
#[derive(Debug, PartialEq)]
pub struct S128 {
    pub f: u128,
}

#[derive(Deserialize, Serialize)]
#[serde(untagged)]
#[derive(Debug, PartialEq)]
pub enum E128 {
    S(S128),
}


#[cfg(test)]
mod tests {
    use serde_test::{assert_de_tokens, Token};

    use super::*;

    #[test]
    fn token_test64() {
        let expected_incoming = E64::S(S64 { f: 1 });
        assert_de_tokens(&expected_incoming, &[
            Token::Struct { name: "S64", len: 1},
            Token::Str("f"),
            Token::U64(1),
            Token::StructEnd,
        ]);
    }

    #[test]
    fn token_test128() {
        let expected_incoming = E128::S(S128 { f: 1 });
        assert_de_tokens(&expected_incoming, &[
            Token::Struct { name: "S128", len: 1},
            Token::Str("f"),
            Token::U64(1),
            Token::StructEnd,
        ]);
    }
}

No playground because of serde_test not available.

@WiSaGaN
Copy link
Contributor Author

WiSaGaN commented Nov 26, 2019

Confirmed #1679 and serde-rs/json#586 fixed the issue.

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

Successfully merging a pull request may close this issue.

1 participant