Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json - Is it possible to flatten sub-object fields while parsing with serde_json?

#[serde(rename)] seems to be the right option, but the documentation does not state if it is possible or how to do it.

This JSON object:

{
   "name" : "myobject"
   "info" : 
   {
      "counter" : "3"
      "foo" : "bar"
   }
}

The corresponding flat Rust struct should be:

#[derive(Deserialize)]
struct Object {
    name: String,
    #[serde(rename="info.counter")] // wrong syntax here !!
    count: i32,
    #[serde(rename="info::foo")] // neither this works
    foo: String,
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no built-in way to do this with attributes, but you can write your own Deserialize impl for your Object type that deserializes first to some intermediate helper representation and then rearranges the data into the desired struct.

#[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());
}

The output is:

Object {
    name: "myobject",
    count: 3,
    foo: "bar"
}

There are three materially different places where insignificant nesting arises:

  1. adjacent to other fields
  2. by itself at the top level
  3. by itself below the top level

All three require different approaches. #1 is observed in this question.

To solve #2 or #3, see Is there a way to omit wrapper/root objects when deserializing objects with Serde?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...