#flatten #serde #struct #clone #debugging #foo #u64

serde-value-flatten

Crate to flatten structs based on serde

1 unstable release

0.1.0 Jun 25, 2019

#1940 in Encoding

Download history 76/week @ 2023-12-17 49/week @ 2023-12-24 104/week @ 2023-12-31 96/week @ 2024-01-07 43/week @ 2024-01-14 40/week @ 2024-01-21 72/week @ 2024-01-28 49/week @ 2024-02-04 85/week @ 2024-02-11 123/week @ 2024-02-18 84/week @ 2024-02-25 75/week @ 2024-03-03 77/week @ 2024-03-10 115/week @ 2024-03-17 44/week @ 2024-03-24 91/week @ 2024-03-31

331 downloads per month
Used in serde_cef

BSD-3-Clause

11KB
76 lines

serde-value-flatten

Build Status Latest version Documentation License

Based on serde-value, serde-value-flatten provides a function to flatten any struct which implement serde::Serialize.

Quickstart

You can start using it by first adding it to your Cargo.toml:

[dependencies]
serde = "1.0"
serde_derive = "1.0"
serde_value_flatten = "0.1"

Then, create a structure which implement the serde::Serialize trait and use it with any serde lib.

Example

#[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_flatten::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
}

Feature ovh-ldp

The feature ovh-ldp allow to suffix fields names to suits to the LDP naming conventions.

In your Cargo.toml, set:

[dependencies]
serde = "1.0"
serde_derive = "1.0"
serde_value_flatten = { version = "0.1", features = ["ovh-ldp"] }

Re-run the previous example, and now the output will be :

{
  "_a": "test",
  "_b_float": 0.5,
  "_c_0_long": 5,
  "_c_1_long": 9,
  "_d_a": "subtest",
  "_d_b_double": 695217
}

License: BSD-3-Clause

Dependencies

~340–640KB
~14K SLoC