简体   繁体   中英

Deserializing key and value lists as struct from JSON using serde?

I have JSON like this:

{
    "fieldNames": ["MyInt", "MyFloat", "MyString"],
    "fieldValues": [5, 10.0, "hello"],
}

I want to deserialize into a struct like this:

#[derive(Deserialize)]
struct MyStruct {
    my_int: u64,
    my_float: f64,
    my_string: String,
}

Is there a way to do this with serde? Ideally I would want something like:

#[serde(keys="fieldNames", values="fieldValues")]

Something like this could work. This is using a deserialize_with function which can be invoked from whatever struct contains this one.


#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;

use serde::de::{self, Deserialize, DeserializeOwned, Deserializer};
use serde_json::Value;

#[derive(Deserialize, Debug)]
struct Spease(#[serde(deserialize_with = "names_values")] MyStruct);

#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct MyStruct {
    my_int: u64,
    my_float: f64,
    my_string: String,
}

fn names_values<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
    T: DeserializeOwned,
    D: Deserializer<'de>
{
    #[derive(Deserialize)]
    struct Helper {
        #[serde(rename = "fieldNames")]
        names: Vec<String>,
        #[serde(rename = "fieldValues")]
        values: Vec<Value>,
    }

    // Deserialize a Vec<String> and Vec<Value>.
    let nv = Helper::deserialize(deserializer)?;

    // Zip them together into a map.
    let pairs = Value::Object(nv.names.into_iter().zip(nv.values).collect());

    // Deserialize the output type T.
    T::deserialize(pairs).map_err(de::Error::custom)
}

fn main() {
    let j = r#"{
                 "fieldNames": ["MyInt", "MyFloat", "MyString"],
                 "fieldValues": [5, 10.0, "hello"]
               }"#;

    println!("{:?}", serde_json::from_str::<Spease>(j).unwrap());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM