[][src]Crate bitflags_serde_shim

bitflags crate Serde shims

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

bitflags_serde_shim = "0.2"

Full example:

extern crate serde_derive;
extern crate serde_json;

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;


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);




Implements Serialize and Deserialize for a bitflags! generated structure.