pub struct As<T: ?Sized>(_);
Expand description

Adapter to convert from serde_as to the serde traits.

The As type adapter allows using types which implement DeserializeAs or SerializeAs in place of serde’s with-annotation. The with-annotation allows running custom code when de/serializing, however it is quite inflexible. The traits DeserializeAs/SerializeAs are more flexible, as they allow composition and nesting of types to create more complex de/serialization behavior. However, they are not directly compatible with serde, as they are not provided by serde. The As type adapter makes them compatible, by forwarding the function calls to serialize/deserialize to the corresponding functions serialize_as and deserialize_as.

It is not required to use this type directly. Instead, it is highly encouraged to use the #[serde_as] attribute since it includes further usability improvements. If the use of the use of the proc-macro is not acceptable, then As can be used directly with serde.

#[derive(Deserialize, Serialize)]
// Serialize numbers as sequence of strings, using Display and FromStr
#[serde(with = "As::<Vec<DisplayFromStr>>")]
field: Vec<u8>,

If the normal Deserialize/Serialize traits should be used, the placeholder type Same can be used. It implements DeserializeAs/SerializeAs, when the underlying type implements Deserialize/Serialize.

#[derive(Deserialize, Serialize)]
// Serialize map, turn keys into strings but keep type of value
#[serde(with = "As::<BTreeMap<DisplayFromStr, Same>>")]
field: BTreeMap<u8, i32>,

Implementations

Serialize type T using SerializeAs

The function signature is compatible with serde’s with-annotation.

Deserialize type T using DeserializeAs

The function signature is compatible with serde’s with-annotation.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.