bitflags_serde_shim 0.2.0

Community Driven Serde Shims
Documentation

bitflags crate Serde shims

To enable to bitflags shims, add it to the crate features list:

[dependencies]
bitflags_serde_shim = "0.2"

Full example:

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

#[macro_use]
extern crate bitflags;
#[macro_use] // required for impl_serde_for_bitflags
extern crate bitflags_serde_shim;

bitflags! {
// Note that `impl_serde_for_bitflags` requires the flag type to
// implement `Serialize` and `Deserialize`.
//
// All primitive integer types satisfy this requirement.
pub struct Permission: u32 {
const SEND_MESSAGE = 0x00000001;
const EDIT_MESSAGE = 0x00000002;
const KICK_MEMBER  = 0x00000004;
const BAN_MEMBER   = 0x00000008;
}
}

impl_serde_for_bitflags!(Permission);

fn main() {
let test = Permission::SEND_MESSAGE | Permission::EDIT_MESSAGE;

assert_eq!(serde_json::to_string(&test).unwrap(), "3");

assert_eq!(serde_json::from_str::<Permission>("3").unwrap(), test);

assert!(serde_json::from_str::<Permission>("51").is_err());
}