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 use serde_bytes inside of untagged enum #1217

Closed
dtolnay opened this issue Apr 18, 2018 · 1 comment
Closed

Cannot use serde_bytes inside of untagged enum #1217

dtolnay opened this issue Apr 18, 2018 · 1 comment
Labels

Comments

@dtolnay
Copy link
Member

dtolnay commented Apr 18, 2018

In the following code, S can be deserialized from a JSON array of bytes but the untagged enum E cannot.

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;
extern crate serde_bytes;

#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum E {
    S(S),
}

#[derive(Deserialize, Debug)]
struct S {
    #[serde(with = "serde_bytes")]
    bytes: Vec<u8>,
}

fn main() {
    let j = "{\"bytes\":[5,4,3,2]}";
    println!("{:?}", serde_json::from_str::<S>(j).unwrap());
    println!("{}", serde_json::from_str::<E>(j).unwrap_err());
}
S { bytes: [5, 4, 3, 2] }
data did not match any variant of untagged enum E
@dtolnay dtolnay added the bug label Apr 18, 2018
@dtolnay
Copy link
Member Author

dtolnay commented Apr 19, 2018

Fixed in 3c4961c.

@dtolnay dtolnay closed this as completed Apr 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant