TryConvert

Type Alias TryConvert 

Source
pub type TryConvert<T, F = Id> = Codec<TryInto<T, F>, TryFrom<T, F>>;
Expand description

Adapter to serialize through a TryFrom conversion

The adapter works by attempting a conversion to T and serializing the result using F. When deserializing, a T is deserialized using F and a conversion to the desired type is attempted.

§Example

use serde::{Deserialize, Serialize};
use serde_json::json;

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct Foo {
    #[serde(with = "serdapt::TryConvert::<u32>")]
    c: char,
}

let foo = Foo { c: 'a' };
let serialized = serde_json::to_value(&foo).unwrap();
assert_eq!(serialized, json!({ "c": b'a' }));
let deserialized = serde_json::from_value::<Foo>(serialized).unwrap();
assert_eq!(deserialized, foo);

Aliased Type§

pub struct TryConvert<T, F = Id>(/* private fields */);