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

Deserializing key and value lists as struct from JSON using 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")]

像这样的东西可以工作。这是使用一个 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());
}