Crates.io | serde-single-key-map |
lib.rs | serde-single-key-map |
version | 0.1.0 |
source | src |
created_at | 2019-12-13 07:35:07.086459 |
updated_at | 2019-12-13 07:35:07.086459 |
description | Unwrap a single key map with serde |
homepage | |
repository | https://github.com/messense/serde-single-key-map.git |
max_upload_size | |
id | 188999 |
size | 7,287 |
Unwrap a single key map with serde.
Add it to your Cargo.toml
:
[dependencies]
serde-single-key-map = "0.1"
#[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");
}
This work is released under the MIT license. A copy of the license is provided in the LICENSE file.