Crates.io | flats |
lib.rs | flats |
version | 0.1.0 |
source | src |
created_at | 2018-06-25 04:41:04.133825 |
updated_at | 2018-06-25 04:41:04.133825 |
description | flattens nested structures into a flat single dimension map |
homepage | https://github.com/softprops/flats |
repository | https://github.com/softprops/flats |
max_upload_size | |
id | 71567 |
size | 12,175 |
🥞 flattens nested structures into a flat single dimension map
Add the following to your cargo project's Cargo.toml
file.
[dependencies]
flats = "0.1"
#[macro_use]
extern crate serde_json;
extern crate flats;
use std::collections::BTreeMap;
use flats::{flatten_value, Scalar};
fn main() {
let flat: BTreeMap<String, Scalar> = flatten_value(
json!({
"name": "John Doe",
"address": {
"city": "nyc"
},
"phones": [
"+44 1234567",
"+44 2345678"
]
})
);
let mut expected: BTreeMap<String, Scalar> = BTreeMap::new();
expected.insert("name".into(), "John Doe".into());
expected.insert("address.city".into(), "nyc".into());
expected.insert("phones[0]".into(), "+44 1234567".into());
expected.insert("phones[1]".into(), "+44 2345678".into());
assert_eq!(expected, flat);
}
Doug Tangren (softprops) 2018