Skip to main content

Module serde_hex

Module serde_hex 

Expand description

Configurable hex Serde representation for opaque bytes. Serialize byte fields as hex strings, with one adapter for vectors and arrays.

The default is lowercase without a prefix. Use upper, prefixed, or upper_prefixed for common alternatives, or colon / upper_colon for colon-separated bytes. Deserialization accepts either digit case and requires the selected prefix and separators exactly.

#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct Digest {
    #[serde(with = "rama_utils::bytes::serde_hex::upper_prefixed")]
    bytes: [u8; 2],
}
let digest = Digest { bytes: [0, 0xab] };
let json = serde_json::to_string(&digest)?;
assert_eq!(json, r#"{"bytes":"0x00AB"}"#);
assert_eq!(serde_json::from_str::<Digest>(&json)?, digest);

For custom formats, use [crate::hex::serde_with!]. Serialization uses Serializer::collect_str; whether it allocates depends on the serializer. Deserializing an array needs no intermediate vector.

Modules§

__serde
Serde
colon
Lowercase colon-separated bytes, without a prefix.
prefixed
Lowercase hex with a required 0x prefix.
upper
Uppercase hex without a prefix.
upper_colon
Uppercase colon-separated bytes, without a prefix.
upper_prefixed
Uppercase hex with a required lowercase 0x prefix.

Functions§

deserialize
Deserialize compact, mixed-case hex into a vector, array, or FromHex type.
serialize
Serialize byte-like input as lowercase, unprefixed hex.