//! Hex encoding with `serde`. #[cfg_attr(
all(feature = "alloc", feature = "serde"),
doc = r##" # Example
``` use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)] struct Foo { #[serde(with = "hex")]
bar: Vec<u8>,
}
``` "##
)] use serde::de::{Error, Visitor}; use serde::Deserializer; #[cfg(feature = "alloc")] use serde::Serializer;
#[cfg(feature = "alloc")] use alloc::string::String;
use core::fmt; use core::marker::PhantomData;
usecrate::FromHex;
#[cfg(feature = "alloc")] usecrate::ToHex;
/// Serializes `data` as hex string using uppercase characters. /// /// Apart from the characters' casing, this works exactly like `serialize()`. #[cfg(feature = "alloc")] pubfn serialize_upper<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
T: ToHex,
{ let s = data.encode_hex_upper::<String>();
serializer.serialize_str(&s)
}
/// Serializes `data` as hex string using lowercase characters. /// /// Lowercase characters are used (e.g. `f9b4ca`). The resulting string's length /// is always even, each byte in data is always encoded using two hex digits. /// Thus, the resulting string contains exactly twice as many bytes as the input /// data. #[cfg(feature = "alloc")] pubfn serialize<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
T: ToHex,
{ let s = data.encode_hex::<String>();
serializer.serialize_str(&s)
}
/// Deserializes a hex string into raw bytes. /// /// Both, upper and lower case characters are valid in the input string and can /// even be mixed (e.g. `f9b4ca`, `F9B4CA` and `f9B4Ca` are all valid strings). pubfn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error> where
D: Deserializer<'de>,
T: FromHex,
<T as FromHex>::Error: fmt::Display,
{ struct HexStrVisitor<T>(PhantomData<T>);
impl<'de, T> Visitor<'de> for HexStrVisitor<T> where
T: FromHex,
<T as FromHex>::Error: fmt::Display,
{ type Value = T;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.