#key #map #serde #deserialize #items

serde-single-key-map

Unwrap a single key map with serde

1 unstable release

0.1.0 Dec 13, 2019

#1534 in Encoding

29 downloads per month
Used in shaco

MIT license

7KB
92 lines

serde-single-key-map

Unwrap a single key map with serde.

Installation

Add it to your Cargo.toml:

[dependencies]
serde-single-key-map = "0.1"

Usage

#[derive(Debug, Deserialize)]
struct Project {
    name: String,
    #[serde(deserialize_with = "serde_single_key_map::deserialize")]
    items: Vec<Item>,
}

#[derive(Debug, Deserialize)]
struct Item {
    name: String,
    source: String,
}

fn main() {
    let s = r#"{
        "name": "test",
        "items": {
            "item": [
            {
                "name": "name",
                "source": "name.rs"
            }
            ]
        }
        }"#;
    let project: Project = serde_json::from_str(s).expect("deserialize failed");
    assert_eq!(project.name, "test");
    assert_eq!(project.items.len(), 1);
    let item = &project.items[0];
    assert_eq!(item.name, "name");
    assert_eq!(item.source, "name.rs");
}

License

This work is released under the MIT license. A copy of the license is provided in the LICENSE file.

Dependencies

~100–335KB