[][src]Crate serde_hex

The serde-hex crate contains various utilities for Serialization/Deserialization of hexadecimal values using serde.

The core utility of this crate is the SerHex trait. Once implemented, SerHex allows for easy configuration of hexadecimal serialization/deserialization with serde-derive:

#[macro_use]
extern crate serde_derive;
extern crate serde_hex;
use serde_hex::{SerHex,StrictPfx};

#[derive(Debug,Serialize,Deserialize)]
struct Foo {
   #[serde(with = "SerHex::<StrictPfx>")]
   bar: [u8;32]
}

The above example will cause serde to serialize Bar into a hexadecimal string with strict sizing (padded with leading zeroes), and prefixing (leading 0x). The possible configurations allow for any combination of strict/compact representations, prefixing, and capitalizing (e.g.; Compact, StrictCapPfx, etc...).

This crate provides implementations of SerHex for all unsigned integer types, as well as generic impls for arrays of types which implement SerHex. The generic impls apply only to strict variants of the trait, and only for arrays of length 1 through 64 (no impl is provided for arrays of length 0 since there isn't really a reasonable way to represent a zero-sized value in hex).

Re-exports

pub use config::*;
pub use types::Error;
pub use types::ParseHexError;

Modules

config

SerHex configuration variants.

macros

Collection of useful macros.

types

Miscellaneous type used by this crate.

utils

various helper functions.

Macros

impl_serhex_bytearray

macro for implementing SerHex for a type which implements From<[u8;n]> and AsRef<[u8]>.

impl_serhex_seq

implement SerHexSeq for a specified type.

Traits

SerHex

Trait specifying custom serialization and deserialization logic from a hexadecimal string to some arbitrary type. This trait can be used to apply custom parsing when using serde's #[derive(Serialize,Deserialize)] flag. Just add #[serde(with = "SerHex")] above any fields which implement this trait. Simplistic default implimentations for the the serialize and deserialize methods are provided based on into_hex_raw and from_hex_raw respectively.

SerHexOpt

Variant of SerHex for serializing/deserializing Option types.

SerHexSeq

Variant of SerHex for serializing/deserializing sequence types as contiguous hexadecimal strings.