不是将字段定义为可选字段,而是收集None
和Some
,如下所示:
extern crate serde;
extern crate serde_json;
use serde::Deserialize;
#[derive(Debug, Deserialize, PartialEq)]
struct Bar {
a: u32,
b: Option<u32>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Foo {
vec: Vec<Bar>,
}
fn main() {
let data = r#"{ "vec": [ { "a": 1 }, { "a": 2, "b": 3 } ] }"#;
assert_eq!(
serde_json::from_str::<Foo>(data).unwrap(),
Foo {
vec: vec![Bar { a: 1, b: None }, Bar { a: 2, b: Some(3) }]
}
);
}
最好只收集完全定义的元素,以便将Bar
结构定义为struct Bar { a: u32, b: u32 }
,而serde_json::from_str
只会返回Foo { vec: [ Bar { a: 2, b: 3 } ] }
。
如何实现这种行为?这是我创建尝试解决此问题的自定义Deserialize
实现的失败尝试。
extern crate serde;
extern crate serde_json;
use core::fmt;
use serde::{
de::{SeqAccess, Visitor},
Deserialize, Deserializer,
};
#[derive(Debug, Deserialize)]
struct Bar {
a: i32,
b: i32,
c: i32,
}
#[derive(Debug)]
struct VecOpt(Vec<Bar>);
impl<'de> Deserialize<'de> for VecOpt {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct ArrayVecVisitor;
impl<'de> Visitor<'de> for ArrayVecVisitor {
type Value = VecOpt;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "VecOpt")
}
fn visit_seq<SA>(self, mut seq: SA) -> Result<Self::Value, SA::Error>
where
SA: SeqAccess<'de>,
{
let mut values = Vec::new();
loop {
match seq.next_element::<Bar>() {
Ok(Some(x)) => values.push(x),
Ok(None) => break,
// If error, the input reader won't move to the next element of the sequence and the following `seq.next_element` will
// simply try to read contents of the current element. In this case, `"c": 5 }, ... `
Err(_) => {}
}
}
Ok(VecOpt(values))
}
}
deserializer.deserialize_seq(ArrayVecVisitor)
}
}
#[derive(Debug, Deserialize)]
struct Foo {
vec: VecOpt,
}
fn main() {
let data = r#"{ "vec": [ { "a": 1 "b": 2, "c": 3 }, { "b": 4, "c": 5 }, { "a": 6 "b": 7, "c": 8 } ] }"#;
dbg!(serde_json::from_str::<Foo>(data).unwrap());
}
答案 0 :(得分:1)
您的代码大部分工作了,但是main
中的JSON文字无效,它错过了几个逗号,将其绊倒了。问题在于Err(_)
分支无差别地吞没了所有错误;它应该只消除了缺少的字段错误:
fn visit_seq<SA>(self, mut seq: SA) -> Result<Self::Value, SA::Error>
where
SA: SeqAccess<'de>,
{
let mut values = Vec::new();
loop {
match seq.next_element::<Bar>() {
Ok(Some(x)) => values.push(x),
Ok(None) => break,
Err(e) => {
if !e.to_string().starts_with("missing field") {
return Err(e);
}
}
}
}
Ok(VecOpt(values))
}
TBH尽管确实有效,但我真的不喜欢该解决方案。 Option
非常适合在此处建模潜在的缺失字段。