#serde #derive #enum #serialize #deserialize #repr

macro serde_repr

Derive Serialize and Deserialize that delegates to the underlying repr of a C-like enum

7 releases

✓ Uses Rust 2018 edition

0.1.6 Jun 16, 2020
0.1.5 Aug 15, 2019
0.1.4 Apr 26, 2019
0.1.3 Feb 21, 2019
0.1.1 Jan 13, 2019

#425 in Encoding

Download history 22870/week @ 2020-03-23 22260/week @ 2020-03-30 18221/week @ 2020-04-06 18133/week @ 2020-04-13 20516/week @ 2020-04-20 20280/week @ 2020-04-27 21875/week @ 2020-05-04 20818/week @ 2020-05-11 19867/week @ 2020-05-18 22012/week @ 2020-05-25 20751/week @ 2020-06-01 21958/week @ 2020-06-08 20758/week @ 2020-06-15 20025/week @ 2020-06-22 19380/week @ 2020-06-29 24184/week @ 2020-07-06

88,317 downloads per month
Used in 124 crates (57 directly)

MIT/Apache

13KB
192 lines

Serde repr derive

github crates.io docs.rs build status

This crate provides a derive macro to derive Serde's Serialize and Deserialize traits in a way that delegates to the underlying repr of a C-like enum.

[dependencies]
serde = "1.0"
serde_repr = "0.1"
use serde_repr::{Serialize_repr, Deserialize_repr};

#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
enum SmallPrime {
    Two = 2,
    Three = 3,
    Five = 5,
    Seven = 7,
}

fn main() -> serde_json::Result<()> {
    let j = serde_json::to_string(&SmallPrime::Seven)?;
    assert_eq!(j, "7");

    let p: SmallPrime = serde_json::from_str("2")?;
    assert_eq!(p, SmallPrime::Two);

    Ok(())
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~385–710KB
~17K SLoC