How to deserialize into a enum variant based on a

2019-05-08 01:20发布

问题:

I have JSON which takes two forms:

"Example:" { "field": 42, "A": 76 }
"Example:" { "field": 42, "B": 110 }

I want to deserialize it into a struct like this:

struct Example {
    field: i32,
    an_enum: AnEnum,
}

where

enum AnEnum {
    A(i32),
    B(i32),
}

I don't know how to do it without writing a custom deserializer for Example.

This works:

"Example:" { "field": 42, "an_enum": {"A": 76} }

or, in YAML:

Example:
    field: 42
    an_enum:
        A: 76

The an_enum is superfluous and annoying to write. How can I deserialize the first form into the struct? Or, alternatively, how can I declare a struct that will successfully deserialize the syntax I want?

回答1:

You are looking for #[serde(flatten)]:

extern crate serde; // 1.0.75
extern crate serde_json; // 1.0.26
#[macro_use]
extern crate serde_derive; // 1.0.75

#[derive(Debug, Deserialize)]
struct Example {
    field: i32,
    #[serde(flatten)]
    an_enum: AnEnum,
}

#[derive(Debug, Deserialize)]
enum AnEnum {
    A(i32),
    B(i32),
}

fn main() {
    let a = r#"{ "field": 42, "A": 76 }"#;
    let b = r#"{ "field": 42, "B": 110 }"#;

    let a = serde_json::from_str::<Example>(a);
    let b = serde_json::from_str::<Example>(b);

    println!("{:?}", a);
    println!("{:?}", b);
}
Ok(Example { field: 42, an_enum: A(76) })
Ok(Example { field: 42, an_enum: B(110) })

Before this was available, I'd use custom deserialization:

extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;

use serde::{Deserialize, Deserializer};
use serde::de::Error;

#[derive(Debug)]
struct Example {
    field: i32,
    an_enum: AnEnum,
}

#[derive(Debug)]
enum AnEnum {
    A(i32),
    B(i32),
}

impl<'de> Deserialize<'de> for Example {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Debug, Deserialize)]
        struct Mapping {
            field: i32,
            #[serde(rename = "A")]
            a: Option<i32>,
            #[serde(rename = "B")]
            b: Option<i32>,
        }

        let Mapping { field, a, b } = Mapping::deserialize(deserializer)?;

        match (a, b) {
            (Some(_), Some(_)) => 
                Err(D::Error::custom("multiple variants specified")),
            (Some(a), None) =>
                Ok(Example { field, an_enum: AnEnum::A(a) }),
            (None, Some(b)) => 
                Ok(Example { field, an_enum: AnEnum::B(b) }),
            (None, None) =>
                Err(D::Error::custom("no variants specified")),
        }
    }
}

fn main() {
    let a = r#"{ "field": 42, "A": 76 }"#;
    let b = r#"{ "field": 42, "B": 110 }"#;

    let a: Result<Example, _> = serde_json::from_str(a);
    let b: Result<Example, _> = serde_json::from_str(b);

    println!("{:?}", a);
    println!("{:?}", b);
}