Skip to main content

serialize

Function serialize 

Source
pub fn serialize<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer, T: AsRef<[u8]>,
Available on crate feature serde only.
Expand description

Serializes a byte view as lowercase hex with a 0x prefix.

Available with serde. This is the default serializer used by #[serde(with = "faster_hex")]. It accepts any [AsRef<[u8]>], reads that view once, and writes a Serde string in every format, including binary formats. Empty bytes serialize as "0x". Use a named policy module to change the prefix or letter case. Temporary storage may allocate; allocation failure follows the allocator’s error handling.

§Errors

Returns the serializer’s error if writing the string fails, or if the encoded length including the prefix cannot be represented as a usize.

§Panics

Panics if temporary output storage would exceed isize::MAX bytes.

§Examples

#[derive(serde::Serialize)]
struct Record {
    #[serde(with = "faster_hex")]
    bytes: Vec<u8>,
}

let value = Record { bytes: vec![0xab, 1] };
assert_eq!(serde_json::to_string(&value)?, r#"{"bytes":"0xab01"}"#);