serde-key-value-vec-map 0.0.1

Deserialize maps or JSON objects in serde to structs that implement the FromKeyValue trait
Documentation

serde-key-value-vec-map

Deserialize maps or JSON objects in serde to structs that implement the FromKeyValue trait.

Example

use serde_key_value_vec_map::*;

#[derive(Debug)]
struct SingleMeasurement {
    name: String,
    value: u32,
}

impl FromKeyValue for SingleMeasurement {
    type Key = String;
    type Value = u32;
    fn from_key_value(key: Self::Key, value: Self::Value) -> Self {
        Self { name: key, value }
    }
}

let json = r#"
    {
        "temperature": 40,
        "pressure": 123
    }
"#;

let values: KeyValueVecMap<SingleMeasurement> =
    serde_json::from_str(json).unwrap();

println!("{:?}", values.0);