如何根据键名反序列化为枚举变体?

How to deserialize into a enum variant based on a key name?

我有 JSON 有两种形式:

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

我想将其反序列化为这样的结构:

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

哪里

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

如果不为 Example 编写自定义反序列化程序,我不知道该怎么做。

这个有效:

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

或者,在 YAML 中:

Example:
    field: 42
    an_enum:
        A: 76

an_enum 是多余的,写起来很烦人。如何将第一种形式反序列化为结构?或者,或者,我如何声明一个能够成功反序列化我想要的语法的结构?

您正在寻找#[serde(flatten)]

use serde::Deserialize; // 1.0.104
use serde_json; // 1.0.48

#[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) })

在此之前,我会使用自定义反序列化:

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);
}