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