#[serde(rename)] 似乎是正确的选择,但文档未说明是否可行或如何执行。
#[serde(rename)]
此JSON对象:
{ "name" : "myobject" "info" : { "counter" : "3" "foo" : "bar" } }
相应的 Flat Rust结构应为:
#[derive(Deserialize)] struct Object { name: String, #[serde(rename="info.counter")] // wrong syntax here !! count: i32, #[serde(rename="info::foo")] // neither this works foo: String, }
没有使用属性的内置方法,但是您可以Deserialize为自己的Object类型编写自己的impl,该类型会先反序列化为中间的辅助表示形式,然后将数据重新排列为所需的结构。
Deserialize
Object
#[macro_use] extern crate serde_derive; extern crate serde; extern crate serde_json; use serde::{Deserialize, Deserializer}; #[derive(Debug)] struct Object { name: String, count: i32, foo: String, } impl<'de> Deserialize<'de> for Object { fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> { #[derive(Deserialize)] struct Outer { name: String, info: Inner, } #[derive(Deserialize)] struct Inner { count: i32, foo: String, } let helper = Outer::deserialize(deserializer)?; Ok(Object { name: helper.name, count: helper.info.count, foo: helper.info.foo, }) } } fn main() { let j = r#"{ "name": "myobject", "info": { "count": 3, "foo": "bar" } }"#; println!("{:#?}", serde_json::from_str::<Object>(j).unwrap()); }
输出为:
Object { name: "myobject", count: 3, foo: "bar" }
在三个明显不同的地方出现了很少的嵌套:
这三个要求不同的方法。在此问题中观察到#1。