gpt4 book ai didi

rust - 反序列化可能是字符串数组或常量字符串的值?

转载 作者:行者123 更新时间:2023-12-05 03:32:11 27 4
gpt4 key购买 nike

我正在使用 Serde 反序列化一些 JSON .我遇到的问题通常是一个字符串数组,但也可以是常量字符串 "all"。以 JSON 模式表示,它看起来像这样:

{
"oneOf": [
{
"const": "all"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
}

我想将它序列化到这个枚举中:

enum MyList {
List(Vec<String>),
All
}

这里有几个例子:

["foo", "bar"]  // Should be MyList::List
"all" // Should be MyList::All
"foo" // Should be an error!

问题是,如何使用 serde 反序列化此枚举?没有container attributesvariant attributes似乎有帮助。如果需要,我可能会更改枚举的设计。但是,JSON 的结构超出了我的控制范围。

最佳答案

这可能与 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

关于rust - 反序列化可能是字符串数组或常量字符串的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70505049/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com