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

Cannot deserialize empty tuple variant #1084

Open
jbearer opened this issue Nov 14, 2023 · 1 comment
Open

Cannot deserialize empty tuple variant #1084

jbearer opened this issue Nov 14, 2023 · 1 comment

Comments

@jbearer
Copy link

jbearer commented Nov 14, 2023

serde_json version 1.0.108
serde version 1.0.192

Deserializing an empty tuple variant of an enum from a Value fails. Deserializing a unit variant succeeds.

#[test]
fn test_serde_json() {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
    enum Test {
        V1,
        V2(),
    }

    for v in [Test::V1, Test::V2()] {
        println!("testing {v:?}");
        let json = serde_json::to_value(&v).unwrap();
        println!("serializes to {json}");
        assert_eq!(v, serde_json::from_value(json).unwrap());
    }
}

Outputs:

testing V1
serializes to "V1"
testing V2
serializes to {"V2":[]}

And then panics when trying to deserialize V2():

called `Result::unwrap()` on an `Err` value: Error("invalid type: null, expected tuple variant Test::V2", line: 0, column: 0)
jbearer added a commit to EspressoSystems/HotShot that referenced this issue Nov 14, 2023
serde_json appears not to handle empty tuple variants correctly,
but it does handle unit variants: serde-rs/json#1084
@zhangboqi
Copy link

hello, how to resolve the problem?

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

2 participants