serde-single-key-map

Crates.ioserde-single-key-map
lib.rsserde-single-key-map
version0.1.0
sourcesrc
created_at2019-12-13 07:35:07.086459
updated_at2019-12-13 07:35:07.086459
descriptionUnwrap a single key map with serde
homepage
repositoryhttps://github.com/messense/serde-single-key-map.git
max_upload_size
id188999
size7,287
messense (messense)

documentation

README

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.

Commit count: 5

cargo fmt