#serde-json #proc-macro #serde-derive #stable #operations #compatible #collection

macro serde-json-helpers

A collection of procedural macros compatible with stable Rust 2018 that simplify some common operations not covered by serde_derive

3 releases

0.1.2 Feb 18, 2019
0.1.1 Feb 17, 2019
0.1.0 Feb 17, 2019

#1303 in Procedural macros

MIT OR Apache-2.0+

23KB
372 lines

Crates.io Build Status

serde-json-helpers

A collection of procedural macros compatible with stable Rust 2018 that simplify some common operations not covered by serde_derive.

Whilst this crate is named serde_json_helpers, serde_json is not a requirement to use it; it's just what the proc macros were intended to be helpful for.

Examples

serde_enum_string

use serde_json_helpers::serde_enum_string;

#[serde_enum_string(transform = "snake_case")]
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
enum ExampleEnum {
    Option1,
    Option2,
    Option3,
    AnotherOption,
}

fn main() {
    let out1 =
        serde_json::to_string(&ExampleEnum::Option1).expect("Unable to serialize ExampleEnum");
    let out2 =
        serde_json::to_string(&ExampleEnum::Option2).expect("Unable to serialize ExampleEnum");
    let out3 =
        serde_json::to_string(&ExampleEnum::Option3).expect("Unable to serialize ExampleEnum");
    let out4 = serde_json::to_string(&ExampleEnum::AnotherOption)
        .expect("Unable to serialize ExampleEnum");

    println!("Serialized ExampleEnum::Option1: {}", out1);
    println!("Serialized ExampleEnum::Option2: {}", out2);
    println!("Serialized ExampleEnum::Option3: {}", out3);
    println!("Serialized ExampleEnum::AnotherOption: {}", out4);

    let in1 = serde_json::from_str::<ExampleEnum>(&*out1).expect("Unable to deserialize");
    let in2 = serde_json::from_str::<ExampleEnum>(&*out2).expect("Unable to deserialize");
    let in3 = serde_json::from_str::<ExampleEnum>(&*out3).expect("Unable to deserialize");
    let in4 = serde_json::from_str::<ExampleEnum>(&*out4).expect("Unable to deserialize");

    println!("Deserialized {}: {:?}", out1, in1);
    println!("Deserialized {}: {:?}", out2, in2);
    println!("Deserialized {}: {:?}", out3, in3);
    println!("Deserialized {}: {:?}", out4, in4);

    println!(
        "Bad deserialized value {}: {:?}",
        "\"bad_value\"",
        serde_json::from_str::<ExampleEnum>("\"bad_value\"")
    );
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.7–1.3MB
~29K SLoC