[][src]Function serde_value_utils::to_flatten_maptree

pub fn to_flatten_maptree<T: ?Sized>(
    key_separator: &str,
    prefix: Option<&str>,
    src: &T
) -> Result<BTreeMap<Value, Value>, SerializerError> where
    T: Serialize

Flatten any struct which implement Serialize into a BTreeMap<serde_value::Value, serde_value::Value> with only one depth.

#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate serde_value_utils;

#[derive(Serialize, Clone, Debug)]
struct SubFoo {
    a: String,
    b: u64,
}

#[derive(Serialize, Clone, Debug)]
struct Foo {
    a: String,
    b: f64,
    c: Vec<i8>,
    d: SubFoo,
}

fn main() {
    let foo = Foo { a: "test".into(), b: 0.5, c: vec![5, 9], d: SubFoo { a: "subtest".into(), b: 695217 } };
    let ser = serde_value_utils::to_flatten_maptree("_", Some("_"), &foo).unwrap();
    println!("{}", serde_json::to_string_pretty(&ser).unwrap());
}

Output:

 {
  "_a": "test",
  "_b": 0.5,
  "_c_0": 5,
  "_c_1": 9,
  "_d_a": "subtest",
  "_d_b": 695217
}