您正在寻找#[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);
}