[−][src]Trait serde::Serializer
A data format that can serialize any data structure supported by Serde.
The role of this trait is to define the serialization half of the Serde
data model, which is a way to categorize every Rust data structure into one
of 29 possible types. Each method of the Serializer
trait corresponds to
one of the types of the data model.
Implementations of Serialize
map themselves into this data model by
invoking exactly one of the Serializer
methods.
The types that make up the Serde data model are:
- 14 primitive types
- bool
- i8, i16, i32, i64, i128
- u8, u16, u32, u64, u128
- 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
Many Serde serializers produce text or binary data as output, for example
JSON or Bincode. This is not a requirement of the Serializer
trait, and
there are serializers that do not produce text or binary output. One example
is the serde_json::value::Serializer
(distinct from the main serde_json
serializer) that produces a serde_json::Value
data structure in memory as
output.
Example implementation
The example data format presented on the website contains example code for
a basic JSON Serializer
.
Associated Types
type Ok
[+]
type Error: Error
[+]
type SerializeSeq: SerializeSeq<Ok = Self::Ok, Error = Self::Error>
[+]
type SerializeTuple: SerializeTuple<Ok = Self::Ok, Error = Self::Error>
[+]
type SerializeTupleStruct: SerializeTupleStruct<Ok = Self::Ok, Error = Self::Error>
[+]
type SerializeTupleVariant: SerializeTupleVariant<Ok = Self::Ok, Error = Self::Error>
[+]
type SerializeMap: SerializeMap<Ok = Self::Ok, Error = Self::Error>
[+]
type SerializeStruct: SerializeStruct<Ok = Self::Ok, Error = Self::Error>
[+]
type SerializeStructVariant: SerializeStructVariant<Ok = Self::Ok, Error = Self::Error>
[+]
Required methods
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_none(self) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> where
T: Serialize,
[+]
T: Serialize,
fn serialize_unit(self) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_unit_struct(
self,
name: &'static str
) -> Result<Self::Ok, Self::Error>
[+]
self,
name: &'static str
) -> Result<Self::Ok, Self::Error>
fn serialize_unit_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str
) -> Result<Self::Ok, Self::Error>
[+]
self,
name: &'static str,
variant_index: u32,
variant: &'static str
) -> Result<Self::Ok, Self::Error>
fn serialize_newtype_struct<T: ?Sized>(
self,
name: &'static str,
value: &T
) -> Result<Self::Ok, Self::Error> where
T: Serialize,
[+]
self,
name: &'static str,
value: &T
) -> Result<Self::Ok, Self::Error> where
T: Serialize,
fn serialize_newtype_variant<T: ?Sized>(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
value: &T
) -> Result<Self::Ok, Self::Error> where
T: Serialize,
[+]
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
value: &T
) -> Result<Self::Ok, Self::Error> where
T: Serialize,
fn serialize_seq(
self,
len: Option<usize>
) -> Result<Self::SerializeSeq, Self::Error>
[+]
self,
len: Option<usize>
) -> Result<Self::SerializeSeq, Self::Error>
fn serialize_tuple(
self,
len: usize
) -> Result<Self::SerializeTuple, Self::Error>
[+]
self,
len: usize
) -> Result<Self::SerializeTuple, Self::Error>
fn serialize_tuple_struct(
self,
name: &'static str,
len: usize
) -> Result<Self::SerializeTupleStruct, Self::Error>
[+]
self,
name: &'static str,
len: usize
) -> Result<Self::SerializeTupleStruct, Self::Error>
fn serialize_tuple_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize
) -> Result<Self::SerializeTupleVariant, Self::Error>
[+]
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize
) -> Result<Self::SerializeTupleVariant, Self::Error>
fn serialize_map(
self,
len: Option<usize>
) -> Result<Self::SerializeMap, Self::Error>
[+]
self,
len: Option<usize>
) -> Result<Self::SerializeMap, Self::Error>
fn serialize_struct(
self,
name: &'static str,
len: usize
) -> Result<Self::SerializeStruct, Self::Error>
[+]
self,
name: &'static str,
len: usize
) -> Result<Self::SerializeStruct, Self::Error>
fn serialize_struct_variant(
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize
) -> Result<Self::SerializeStructVariant, Self::Error>
[+]
self,
name: &'static str,
variant_index: u32,
variant: &'static str,
len: usize
) -> Result<Self::SerializeStructVariant, Self::Error>
Provided methods
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error>
[+]
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error>
[+]
fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error> where
I: IntoIterator,
<I as IntoIterator>::Item: Serialize,
[+]
I: IntoIterator,
<I as IntoIterator>::Item: Serialize,
fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error> where
K: Serialize,
V: Serialize,
I: IntoIterator<Item = (K, V)>,
[+]
K: Serialize,
V: Serialize,
I: IntoIterator<Item = (K, V)>,
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> where
T: Display,
[+]
T: Display,