Skip to main content

Crate deser_encoding

Crate deser_encoding 

Source
Expand description

More encodings of bytes as strings for deser.

deser writes bytes as base64 strings in formats without native bytes (like JSON) and provides the base64 encodings (see bytes in deser). This crate adds hexadecimal and base32:

EncodingDescription
Hexhexadecimal, lowercase
HexUpperhexadecimal, uppercase
Base32base32 with padding
Base32NoPadbase32 without padding
Base32Hexbase32 with extended hex alphabet and padding
Base32HexNoPadbase32 with extended hex alphabet without padding
Base32Dnssecbase32 of DNSSEC, lowercase extended hex alphabet

Like the encodings of deser they are adapters which represent bytes as strings in all formats. They can be used with BytesFallback to keep native bytes in formats that have them, and with BytesFormat to configure formats:

use deser::adapters::BytesFallback;
use deser::{BytesFormat, Deserialize, Serialize};
use deser_encoding::Hex;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Blob {
    // a hex string in all formats
    #[deser(as = Hex)]
    digest: [u8; 4],
    // hex in JSON, bytes in CBOR
    #[deser(as = BytesFallback<Hex>)]
    signature: Vec<u8>,
    // base64 in JSON, bytes in CBOR
    data: Vec<u8>,
}

let blob = Blob {
    digest: [0xde, 0xad, 0xbe, 0xef],
    signature: vec![1, 255],
    data: vec![1, 255],
};
let json = deser_json::to_string(&blob).unwrap();
assert_eq!(
    json,
    r#"{"digest":"deadbeef","signature":"01ff","data":"Af8="}"#
);
assert_eq!(deser_json::from_str::<Blob>(&json).unwrap(), blob);

// all bytes as hex (in the context of the serialization)
let hex = deser_json::SerializerConfig::builder()
    .context(deser::Context::with(BytesFormat::encoded::<Hex>()))
    .build();
assert_eq!(hex.to_string(&b"\x01\xff").unwrap(), r#""01ff""#);

All encodings decode lowercase and uppercase letters.

Structsยง

Base32
Base32 with padding (RFC 4648 section 6).
Base32Dnssec
Base32 of DNSSEC (RFC 5155 section 3.3): the extended hex alphabet with lowercase letters and without padding.
Base32Hex
Base32 with the extended hex alphabet and padding (RFC 4648 section 7).
Base32HexNoPad
Base32 with the extended hex alphabet without padding.
Base32NoPad
Base32 without padding.
Hex
Hexadecimal with lowercase digits.
HexUpper
Hexadecimal with uppercase digits.