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

```rust use serdejsonhelpers::serdeenumstring;

[serdeenumstring(transform = "snake_case")]

[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]

enum ExampleEnum { Option1, Option2, Option3, AnotherOption, }

fn main() { let out1 = serdejson::tostring(&ExampleEnum::Option1).expect("Unable to serialize ExampleEnum"); let out2 = serdejson::tostring(&ExampleEnum::Option2).expect("Unable to serialize ExampleEnum"); let out3 = serdejson::tostring(&ExampleEnum::Option3).expect("Unable to serialize ExampleEnum"); let out4 = serdejson::tostring(&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.