#value #serde #utilities #debugging

serde-value-utils

Bundle of utils for serde-value

1 unstable release

0.1.0 Jul 3, 2019

#62 in #utils

Download history 116/week @ 2024-03-27 157/week @ 2024-04-03 155/week @ 2024-04-10 164/week @ 2024-04-17 180/week @ 2024-04-24 100/week @ 2024-05-01 213/week @ 2024-05-08 202/week @ 2024-05-15 232/week @ 2024-05-22 157/week @ 2024-05-29 242/week @ 2024-06-05 106/week @ 2024-06-12 121/week @ 2024-06-19 128/week @ 2024-06-26 114/week @ 2024-07-03 107/week @ 2024-07-10

504 downloads per month
Used in 2 crates (via serde_gelf)

BSD-3-Clause

14KB
93 lines

serde-value-utils

Build Status Latest version Documentation License

Bundle of tools to use with serde_value.

Quickstart

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

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

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

Example: to_flatten_maptree

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

Feature with-schema

The feature with-schema allow to suffix fields names to suits to the LDP naming conventions.

In your Cargo.toml, set:

[dependencies]
serde_value_utils = { version = "0.1", features = ["with-schema"] }

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
}

Example: try_detect_type

extern crate serde_value_utils;

use serde_value_utils::try_detect_type;

fn main() {
    println!("{:?}", try_detect_type("6.5"));
}

Output:

F64(6.5)

License: BSD-3-Clause

Dependencies

~335–640KB
~14K SLoC