Trait serde::Deserializer
[−]
[src]
pub trait Deserializer<'de>: Sized { type Error: Error; fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_byte_buf<V>(self,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_unit_struct<V>(self,
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_newtype_struct<V>(self,
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_tuple<V>(self,
len: usize,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_tuple_struct<V>(self,
name: &'static str,
len: usize,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_struct<V>(self,
name: &'static str,
fields: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_enum<V>(self,
name: &'static str,
variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_identifier<V>(self,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; fn deserialize_ignored_any<V>(self,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>; }
A data format that can deserialize any data structure supported by Serde.
The role of this trait is to define the deserialization half of the Serde
data model, which is a way to categorize every Rust data type into one of 27
possible types. Each method of the Serializer
trait corresponds to one of
the types of the data model.
Implementations of Deserialize
map themselves into this data model by
passing to the Deserializer
a Visitor
implementation that can receive
these various types.
The types that make up the Serde data model are:
- 12 primitive types
- bool
- i8, i16, i32, i64
- u8, u16, u32, u64
- f32, f64
- char
- string
- UTF-8 bytes with a length and no null terminator.
- When serializing, all strings are handled equally. When deserializing, there are three flavors of strings: transient, owned, and borrowed.
- byte array - [u8]
- Similar to strings, during deserialization byte arrays can be transient, owned, or borrowed.
- option
- Either none or some value.
- unit
- The type of
()
in Rust. It represents an anonymous value containing no data.
- The type of
- unit_struct
- For example
struct Unit
orPhantomData<T>
. It represents a named value containing no data.
- For example
- unit_variant
- For example the
E::A
andE::B
inenum E { A, B }
.
- For example the
- newtype_struct
- For example
struct Millimeters(u8)
.
- For example
- newtype_variant
- For example the
E::N
inenum E { N(u8) }
.
- For example the
- seq
- A variably sized heterogeneous sequence of values, for example
Vec<T>
orHashSet<T>
. When serializing, the length may or may not be known before iterating through all the data. When deserializing, the length is determined by looking at the serialized data.
- A variably sized heterogeneous sequence of values, for example
- tuple
- A statically sized heterogeneous sequence of values for which the length
will be known at deserialization time without looking at the serialized
data, for example
(u8,)
or(String, u64, Vec<T>)
or[u64; 10]
.
- A statically sized heterogeneous sequence of values for which the length
will be known at deserialization time without looking at the serialized
data, for example
- tuple_struct
- A named tuple, for example
struct Rgb(u8, u8, u8)
.
- A named tuple, for example
- tuple_variant
- For example the
E::T
inenum E { T(u8, u8) }
.
- For example the
- map
- A heterogeneous key-value pairing, for example
BTreeMap<K, V>
.
- A heterogeneous key-value pairing, for example
- struct
- A heterogeneous key-value pairing in which the keys are strings and will be
known at deserialization time without looking at the serialized data, for
example
struct S { r: u8, g: u8, b: u8 }
.
- A heterogeneous key-value pairing in which the keys are strings and will be
known at deserialization time without looking at the serialized data, for
example
- struct_variant
- For example the
E::S
inenum E { S { r: u8, g: u8, b: u8 } }
.
- For example the
The Deserializer
trait supports two entry point styles which enables
different kinds of deserialization.
The
deserialize
method. Self-describing data formats like JSON are able to look at the serialized data and tell what it represents. For example the JSON deserializer may see an opening curly brace ({
) and know that it is seeing a map. If the data format supportsDeserializer::deserialize_any
, it will drive the Visitor using whatever type it sees in the input. JSON uses this approach when deserializingserde_json::Value
which is an enum that can represent any JSON document. Without knowing what is in a JSON document, we can deserialize it toserde_json::Value
by going throughDeserializer::deserialize_any
.The various
deserialize_*
methods. Non-self-describing formats like Bincode need to be told what is in the input in order to deserialize it. Thedeserialize_*
methods are hints to the deserializer for how to interpret the next piece of input. Non-self-describing formats are not able to deserialize something likeserde_json::Value
which relies onDeserializer::deserialize_any
.
When implementing Deserialize
, you should avoid relying on
Deserializer::deserialize_any
unless you need to be told by the Deserializer
what type is in the input. Know that relying on Deserializer::deserialize_any
means your data type will be able to deserialize from self-describing
formats only, ruling out Bincode and many others.
Associated Types
Required Methods
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Require the Deserializer
to figure out how to drive the visitor based
on what data type is in the input.
When implementing Deserialize
, you should avoid relying on
Deserializer::deserialize_any
unless you need to be told by the
Deserializer what type is in the input. Know that relying on
Deserializer::deserialize_any
means your data type will be able to
deserialize from self-describing formats only, ruling out Bincode and
many others.
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a bool
value.
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting an i8
value.
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting an i16
value.
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting an i32
value.
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting an i64
value.
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a u8
value.
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a u16
value.
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a u32
value.
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a u64
value.
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a f32
value.
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a f64
value.
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a char
value.
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a string value and does
not benefit from taking ownership of buffered data owned by the
Deserializer
.
If the Visitor
would benefit from taking ownership of String
data,
indiciate this to the Deserializer
by using deserialize_string
instead.
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a string value and would
benefit from taking ownership of buffered data owned by the
Deserializer
.
If the Visitor
would not benefit from taking ownership of String
data, indicate that to the Deserializer
by using deserialize_str
instead.
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a byte array and does not
benefit from taking ownership of buffered data owned by the
Deserializer
.
If the Visitor
would benefit from taking ownership of Vec<u8>
data,
indicate this to the Deserializer
by using deserialize_byte_buf
instead.
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a byte array and would
benefit from taking ownership of buffered data owned by the
Deserializer
.
If the Visitor
would not benefit from taking ownership of Vec<u8>
data, indicate that to the Deserializer
by using deserialize_bytes
instead.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting an optional value.
This allows deserializers that encode an optional value as a nullable
value to convert the null value into None
and a regular value into
Some(value)
.
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a unit value.
fn deserialize_unit_struct<V>(self,
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a unit struct with a
particular name.
fn deserialize_newtype_struct<V>(self,
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
name: &'static str,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a newtype struct with a
particular name.
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a sequence of values.
fn deserialize_tuple<V>(self,
len: usize,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
len: usize,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a sequence of values and
knows how many values there are without looking at the serialized data.
fn deserialize_tuple_struct<V>(self,
name: &'static str,
len: usize,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
name: &'static str,
len: usize,
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a tuple struct with a
particular name and number of fields.
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a map of key-value pairs.
fn deserialize_struct<V>(self,
name: &'static str,
fields: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
name: &'static str,
fields: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting a struct with a particular
name and fields.
fn deserialize_enum<V>(self,
name: &'static str,
variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
name: &'static str,
variants: &'static [&'static str],
visitor: V)
-> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting an enum value with a
particular name and possible variants.
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type is expecting the name of a struct
field or the discriminant of an enum variant.
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>
Hint that the Deserialize
type needs to deserialize a value whose type
doesn't matter because it is ignored.
Deserializers for non-self-describing formats may not support this mode.
Implementors
impl<'de, E> Deserializer<'de> for UnitDeserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for BoolDeserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for I8Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for I16Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for I32Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for I64Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for IsizeDeserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for U8Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for U16Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for U64Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for UsizeDeserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for F32Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for F64Deserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for CharDeserializer<E> where E: Error
impl<'de, E> Deserializer<'de> for U32Deserializer<E> where E: Error
impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E> where E: Error
impl<'de, E> Deserializer<'de> for BorrowedStrDeserializer<'de, E> where E: Error
impl<'de, E> Deserializer<'de> for StringDeserializer<E> where E: Error
impl<'de, 'a, E> Deserializer<'de> for CowStrDeserializer<'a, E> where E: Error
impl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'de, E> where E: Error
impl<'de, I, T, E> Deserializer<'de> for SeqDeserializer<I, E> where I: Iterator<Item=T>, T: IntoDeserializer<'de, E>, E: Error
impl<'de, A> Deserializer<'de> for SeqAccessDeserializer<A> where A: SeqAccess<'de>
impl<'de, I, E> Deserializer<'de> for MapDeserializer<'de, I, E> where I: Iterator,
I::Item: Pair,
I::Item::First: IntoDeserializer<'de, E>,
I::Item::Second: IntoDeserializer<'de, E>,
E: Errorimpl<'de, A> Deserializer<'de> for MapAccessDeserializer<A> where A: MapAccess<'de>