macro_rules! enum_number_declare {
($visibility:vis $name:ident { $($variant:ident = $value:expr, )* }) => { ... };
}
Expand description
This contains both serialization and ser/deserialization of a enum into and from numbers.
The reference implementation does not work if your
enum has negative values. This enum_number
handles this also.
ยงExample
serde_aux::enum_number_declare!(pub TestEnum {
Up = 1,
None = 0,
Down = -1,
});
let s = r#"1"#;
let a: TestEnum = serde_json::from_str(s).unwrap();
assert_eq!(a, TestEnum::Up);
let s = r#"0"#;
let a: TestEnum = serde_json::from_str(s).unwrap();
assert_eq!(a, TestEnum::None);
let s = r#"-1"#;
let a: TestEnum = serde_json::from_str(s).unwrap();
assert_eq!(a, TestEnum::Down);
let s = r#"5"#;
assert!(serde_json::from_str::<TestEnum>(s).is_err());