serde_hex

Trait SerHexOpt

Source
pub trait SerHexOpt<C>: Sized + SerHex<C>
where C: HexConf,
{ // Provided methods fn serialize<S>( option: &Option<Self>, serializer: S, ) -> Result<S::Ok, S::Error> where S: Serializer { ... } fn deserialize<'de, D>(deserializer: D) -> Result<Option<Self>, D::Error> where D: Deserializer<'de> { ... } }
Expand description

Variant of SerHex for serializing/deserializing Option types.

Any type T which implements SerHex<C> implements SerHexOpt<C> automatically.

#[derive(Debug,PartialEq,Eq,Serialize,Deserialize)]
struct MaybeNum {
    #[serde(with = "SerHexOpt::<CompactPfx>")]
    num: Option<u64>
}

let s: MaybeNum = serde_json::from_str(r#"{"num":"0xff"}"#).unwrap();
assert_eq!(s,MaybeNum { num: Some(255) });

let n: MaybeNum = serde_json::from_str(r#"{"num":null}"#).unwrap();
assert_eq!(n,MaybeNum { num: None });

Provided Methods§

Source

fn serialize<S>(option: &Option<Self>, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Same as SerHex::serialize, except for Option<Self> instead of Self.

Source

fn deserialize<'de, D>(deserializer: D) -> Result<Option<Self>, D::Error>
where D: Deserializer<'de>,

Same as SerHex::deserialize, except for Option<Self> instead of Self.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, C> SerHexOpt<C> for T
where T: Sized + SerHex<C>, C: HexConf,