[][src]Struct serde_with::hex::Hex

pub struct Hex<FORMAT: Format = Lowercase>(_);

Serialize bytes as a hex string

The type serializes a sequence of bytes as a hexadecimal string. It works on any type implementing AsRef<[u8]> for serialization and From<Vec<u8>> for deserialization.

The format type parameter specifies if the hex string should use lower- or uppercase characters. Valid options are the types Lowercase and Uppercase. Deserialization always supports lower- and uppercase characters, even mixed in one string.

Example


#[serde_as]
#[derive(Deserialize, Serialize)]
struct BytesLowercase(
    // Equivalent to serde_with::hex::Hex<serde_with::formats::Lowercase>
    #[serde_as(as = "serde_with::hex::Hex")]
    Vec<u8>
);

#[serde_as]
#[derive(Deserialize, Serialize)]
struct BytesUppercase(
    #[serde_as(as = "serde_with::hex::Hex<serde_with::formats::Uppercase>")]
    Vec<u8>
);

let b = b"Hello World!";

// Hex with lowercase letters
assert_eq!(json!("48656c6c6f20576f726c6421"), serde_json::to_value(BytesLowercase(b.to_vec())).unwrap());
// Hex with uppercase letters
assert_eq!(json!("48656C6C6F20576F726C6421"), serde_json::to_value(BytesUppercase(b.to_vec())).unwrap());

// Serialization always work from lower- and uppercase characters, even mixed case.
assert_eq!(BytesLowercase(vec![0x00, 0xaa, 0xbc, 0x99, 0xff]), serde_json::from_value(json!("00aAbc99FF")).unwrap());
assert_eq!(BytesUppercase(vec![0x00, 0xaa, 0xbc, 0x99, 0xff]), serde_json::from_value(json!("00aAbc99FF")).unwrap());

Trait Implementations

impl<FORMAT: Clone + Format> Clone for Hex<FORMAT>[src]

impl<FORMAT: Copy + Format> Copy for Hex<FORMAT>[src]

impl<FORMAT: Debug + Format> Debug for Hex<FORMAT>[src]

impl<FORMAT: Default + Format> Default for Hex<FORMAT>[src]

impl<'de, T, FORMAT> DeserializeAs<'de, T> for Hex<FORMAT> where
    T: From<Vec<u8>>,
    FORMAT: Format
[src]

impl<T> SerializeAs<T> for Hex<Lowercase> where
    T: AsRef<[u8]>, 
[src]

impl<T> SerializeAs<T> for Hex<Uppercase> where
    T: AsRef<[u8]>, 
[src]

Auto Trait Implementations

impl<FORMAT> RefUnwindSafe for Hex<FORMAT> where
    FORMAT: RefUnwindSafe

impl<FORMAT> Send for Hex<FORMAT> where
    FORMAT: Send

impl<FORMAT> Sync for Hex<FORMAT> where
    FORMAT: Sync

impl<FORMAT> Unpin for Hex<FORMAT> where
    FORMAT: Unpin

impl<FORMAT> UnwindSafe for Hex<FORMAT> where
    FORMAT: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.