gpt4 book ai didi

json - 无法使用 Rustc-serialize 处理 JSON 中的可选字段

转载 作者:行者123 更新时间:2023-11-29 07:44:47 26 4
gpt4 key购买 nike

我正在尝试使用 rustc_serialize 将 JSON 反序列化为 Rust 结构。问题是某些 JSON 有一些可选字段,即可能存在也可能不存在。遇到第一个不存在的字段时,解码器似乎会退出并且不考虑后续字段,即使它们存在也是如此。有办法克服这个问题吗?

代码如下:

extern crate rustc_serialize;

#[derive(Debug)]
struct B {
some_field_0: Option<u64>,
some_field_1: Option<String>,
}

impl rustc_serialize::Decodable for B {
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
Ok(B {
some_field_0: d.read_struct_field("some_field_0", 0, |d| rustc_serialize::Decodable::decode(d)).ok(),
some_field_1: d.read_struct_field("some_field_1", 0, |d| rustc_serialize::Decodable::decode(d)).ok(),
})
}
}

fn main() {
{
println!("--------------------------------\n1st run - all field present\n--------------------------------");
let json_str = "{\"some_field_0\": 1234, \"some_field_1\": \"There\"}".to_string();
let obj_b: B = rustc_serialize::json::decode(&json_str).unwrap();

println!("\nJSON: {}\nDecoded: {:?}", json_str, obj_b);
}

{
println!("\n\n--------------------------------\n2nd run - \"some_field_1\" absent\n---------------------------------");
let json_str = "{\"some_field_0\": 1234}".to_string();
let obj_b: B = rustc_serialize::json::decode(&json_str).unwrap();

println!("\nJSON: {}\nDecoded: {:?}", json_str, obj_b);
}

{
println!("\n\n--------------------------------\n3rd run - \"some_field_0\" absent\n---------------------------------");
let json_str = "{\"some_field_1\": \"There\"}".to_string();
let obj_b: B = rustc_serialize::json::decode(&json_str).unwrap();

println!("\nJSON: {}\nDecoded: {:?}", json_str, obj_b);
}
}

这是输出:

--------------------------------
1st run - all field present
--------------------------------

JSON: {"some_field_0": 1234, "some_field_1": "There"}
Decoded: B { some_field_0: Some(1234), some_field_1: Some("There") }


--------------------------------
2nd run - "some_field_1" absent
---------------------------------

JSON: {"some_field_0": 1234}
Decoded: B { some_field_0: Some(1234), some_field_1: None }


--------------------------------
3rd run - "some_field_0" absent
---------------------------------

JSON: {"some_field_1": "There"}
Decoded: B { some_field_0: None, some_field_1: None }

请注意,第三次运行产生了意想不到的结果。当解码器无法找到 some_field_0 时,即使 some_field_1 存在,它也会在所有后续标记上失败。

最佳答案

您的Decodable 实现有问题。使用自动生成的实现工作:

#[derive(Debug, RustcDecodable)]
struct B {
some_field_1: Option<String>,
some_field_0: Option<u64>,
}
JSON: {"some_field_1": "There"}
Decoded: B { some_field_1: Some("There"), some_field_0: None }

如果可以,使用生成的实现是正确的做法。如果你不能,这里是正确的实现:

impl rustc_serialize::Decodable for B {
fn decode<D: rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
Ok(B {
some_field_0: try!(d.read_struct_field("some_field_0", 0, |d| rustc_serialize::Decodable::decode(d))),
some_field_1: try!(d.read_struct_field("some_field_1", 0, |d| rustc_serialize::Decodable::decode(d))),
})
}
}

重要的变化是 try! 的使用。解码可能会失败。通过使用 ok,您是说解码失败实际上是成功,尽管是对 None 的成功解码。

关于json - 无法使用 Rustc-serialize 处理 JSON 中的可选字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411991/

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