使用serde将键和值列表反序列化为来自JSON的结构体?

时间:2017-11-21 04:48:04

标签: json rust serde

我有这样的JSON:

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

我想反序列化为这样的结构:

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

有没有办法用serde做到这一点?理想情况下,我想要像:

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

1 个答案:

答案 0 :(得分:5)

这样的事情可行。这是使用deserialize_with函数,可以从包含此函数的任何结构中调用它。

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