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

Support deserializing a flattened internally tagged enum #1189

Closed
dtolnay opened this issue Mar 22, 2018 · 1 comment
Closed

Support deserializing a flattened internally tagged enum #1189

dtolnay opened this issue Mar 22, 2018 · 1 comment
Assignees

Comments

@dtolnay
Copy link
Member

dtolnay commented Mar 22, 2018

This would be valuable to support if possible. Serialization already works but deserialization does not.

#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct Data {
    id: i32,
    #[serde(flatten)]
    payload: Enum,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
enum Enum {
    A(A),
    B(B),
}

#[derive(Serialize, Deserialize, Debug)]
struct A {
    field_a: i32,
}

#[derive(Serialize, Deserialize, Debug)]
struct B {
    field_b: i32,
}

fn main() {
    let data = Data {
        id: 0,
        payload: Enum::A(A { field_a: 0 }),
    };

    let j = serde_json::to_string(&data).unwrap();
    println!("{}", j);

    println!("{}", serde_json::from_str::<Data>(&j).unwrap_err());
}
{"id":0,"type":"A","field_a":0}
can only flatten structs and maps at line 1 column 31
@dtolnay
Copy link
Member Author

dtolnay commented May 6, 2018

@mammothbane @Nemikolh @weiwei-lin @georgemarshall @PlasmaPower @jplatte
this is now supported in 1.0.46!

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

No branches or pull requests

2 participants