untagged enum representation 和 deserialize_with variant attribute 的组合是可能的:
use serde::{Deserialize, Deserializer};
#[derive(Debug, Deserialize)]
// This annotation "flattens" the enum, allowing one to treat a list of strings
// directly as a List variant, without extra annotations.
// Serde will attempt to deserialize input as each variant in order,
// returning an error if no one matches.
#[serde(untagged)]
enum MyList {
// This annotation delegates deserialization of this variant to own code,
// since otherwise Serde wouldn't know what to do with the string.
#[serde(deserialize_with = "all")]
All,
// This variant is deserialized as usual.
List(Vec<String>),
}
// A custom deserialization function, referred to by `deserialize_with`.
// (reference: https://github.com/serde-rs/serde/issues/1158)
fn all<'de, D>(deserializer: D) -> Result<(), D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
// This enum is, by default, "externally tagged";
// but, since it consists only of a single unit variant,
// it means that it can be deserialized only from
// the corresponding constant string - and that's exactly what we need
enum Helper {
#[serde(rename = "all")]
Variant,
}
// We're not interested in the deserialized value (we know what it is),
// so we can simply map it to (), as required by signature
Helper::deserialize(deserializer).map(|_| ())
}
fn main() {
// Trying to parse an array with both variants...
let json = r#"["all", ["a", "b", "c"]]"#;
let lists: Vec<MyList> = serde_json::from_str(json).expect("cannot parse");
// ...we can see that it is indeed parsed correctly...
println!("{:?}", lists);
// ...and any unexpected string results in an error
serde_json::from_str::<MyList>(r#""foo""#).unwrap_err();
}
Playground