Convert

Type Alias Convert 

Source
pub type Convert<T, F = Id> = Codec<Into<T, F>, From<T, F>>;
Expand description

Adapter to serialize through a From conversion

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

§Example

use serde::{Deserialize, Serialize};
use serde_json::json;
use std::net::Ipv4Addr;

#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct Foo(#[serde(with = "serdapt::Convert::<u32>")] Ipv4Addr);

let foo = Foo(Ipv4Addr::new(127, 0, 0, 1));
let serialized = serde_json::to_value(&foo).unwrap();
assert_eq!(serialized, json!(0x7f000001));
let deserialized = serde_json::from_value::<Foo>(serialized).unwrap();
assert_eq!(deserialized, foo);

Aliased Type§

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