[][src]Crate enum_primitive_serde_shim

enum_primitive crate Serde shims

To enable this shim, add it to the crate features list:

[dependencies]
enum_primitive_serde_shim = "0.2"

Full example:

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

#[macro_use]
extern crate enum_primitive;
#[macro_use] // required for impl_serde_for_enum_primitive
extern crate enum_primitive_serde_shim;

enum_from_primitive! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    enum Codes {
        CodeA = 0,
        CodeB = 1,
        CodeC = 5,
        CodeD = 8,
    }
}

impl_serde_for_enum_primitive!(Codes);

fn main() {
    let code = Codes::CodeC;

    assert_eq!(serde_json::to_string(&code).unwrap(), "5");

    assert_eq!(serde_json::from_str::<Codes>("8").unwrap(), Codes::CodeD);

    assert!(serde_json::from_str::<Codes>("16").is_err());
}

Macros

impl_serde_for_enum_primitive

Implements Serialize and Deserialize for an enum_from_primitive! generated enum.