#serialization #enums #integer #serde #no-alloc #serialize-and-deserialize

macro no-std serde_short

Derive Serialize and Deserialize for enum reperesented as C short enum

4 releases

0.1.3 Jan 9, 2025
0.1.2 Sep 2, 2024
0.1.1 Sep 2, 2024
0.1.0 Sep 2, 2024

#1322 in Encoding

Download history 2377/week @ 2025-02-16 3567/week @ 2025-02-23 3918/week @ 2025-03-02 3829/week @ 2025-03-09 3422/week @ 2025-03-16 3569/week @ 2025-03-23 2185/week @ 2025-03-30 1468/week @ 2025-04-06 1147/week @ 2025-04-13 950/week @ 2025-04-20 867/week @ 2025-04-27 1173/week @ 2025-05-04 1173/week @ 2025-05-11 1698/week @ 2025-05-18 1289/week @ 2025-05-25 1577/week @ 2025-06-01

5,789 downloads per month

MIT license

14KB
249 lines

Derive Serialize and Deserialize that uses short enum representation for C enum.

A small variant of the serde-repr (99% of the code is simple a copy/paste)

Examples

use serde_short::{Serialize_short, Deserialize_short};

#[derive(Serialize_short, Deserialize_short, PartialEq, Debug)]
#[repr(C)]
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(())
}

Serde short derive

This crate provides a derive macro to derive Serde's Serialize and Deserialize traits for C enum represented as short enum u8, u16, u32 or in case first value is < 0 i8, i16 or i32

99% of the code is a copy/paste from serde-repr all credits goes to this crate

[dependencies]
serde = "1.0"
serde_short = "0.1"
use serde_short::{Serialize_short, Deserialize_short};

#[derive(Serialize_short, Deserialize_short, 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(())
}

Credits

Dependencies

~190–620KB
~15K SLoC