pub trait Serializer:Sized { typeOk; typeError:Error; typeSerializeSeq:SerializeSeq<Ok = Self::Ok, Error = Self::Error>; typeSerializeTuple:SerializeTuple<Ok = Self::Ok, Error = Self::Error>; typeSerializeTupleStruct:SerializeTupleStruct<Ok = Self::Ok, Error = Self::Error>; typeSerializeTupleVariant:SerializeTupleVariant<Ok = Self::Ok, Error = Self::Error>; typeSerializeMap:SerializeMap<Ok = Self::Ok, Error = Self::Error>; typeSerializeStruct:SerializeStruct<Ok = Self::Ok, Error = Self::Error>; typeSerializeStructVariant:SerializeStructVariant<Ok = Self::Ok, Error = Self::Error>;Show 34 methods
// Required methods fnserialize_bool(self, v:bool) ->Result<Self::Ok, Self::Error>; fnserialize_i8(self, v:i8) ->Result<Self::Ok, Self::Error>; fnserialize_i16(self, v:i16) ->Result<Self::Ok, Self::Error>; fnserialize_i32(self, v:i32) ->Result<Self::Ok, Self::Error>; fnserialize_i64(self, v:i64) ->Result<Self::Ok, Self::Error>; fnserialize_u8(self, v:u8) ->Result<Self::Ok, Self::Error>; fnserialize_u16(self, v:u16) ->Result<Self::Ok, Self::Error>; fnserialize_u32(self, v:u32) ->Result<Self::Ok, Self::Error>; fnserialize_u64(self, v:u64) ->Result<Self::Ok, Self::Error>; fnserialize_f32(self, v:f32) ->Result<Self::Ok, Self::Error>; fnserialize_f64(self, v:f64) ->Result<Self::Ok, Self::Error>; fnserialize_char(self, v:char) ->Result<Self::Ok, Self::Error>; fnserialize_str(self, v: &str) ->Result<Self::Ok, Self::Error>; fnserialize_bytes(self, v: &[u8]) ->Result<Self::Ok, Self::Error>; fnserialize_none(self) ->Result<Self::Ok, Self::Error>; fnserialize_some<T>(self, value:&T) ->Result<Self::Ok, Self::Error>where T: ?Sized +Serialize; fnserialize_unit(self) ->Result<Self::Ok, Self::Error>; fnserialize_unit_struct( self, name: &'staticstr, ) ->Result<Self::Ok, Self::Error>; fnserialize_unit_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, ) ->Result<Self::Ok, Self::Error>; fnserialize_newtype_struct<T>( self, name: &'staticstr, value:&T, ) ->Result<Self::Ok, Self::Error>where T: ?Sized +Serialize; fnserialize_newtype_variant<T>( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, value:&T, ) ->Result<Self::Ok, Self::Error>where T: ?Sized +Serialize; fnserialize_seq( self, len:Option<usize>, ) ->Result<Self::SerializeSeq, Self::Error>; fnserialize_tuple( self, len:usize, ) ->Result<Self::SerializeTuple, Self::Error>; fnserialize_tuple_struct( self, name: &'staticstr, len:usize, ) ->Result<Self::SerializeTupleStruct, Self::Error>; fnserialize_tuple_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, len:usize, ) ->Result<Self::SerializeTupleVariant, Self::Error>; fnserialize_map( self, len:Option<usize>, ) ->Result<Self::SerializeMap, Self::Error>; fnserialize_struct( self, name: &'staticstr, len:usize, ) ->Result<Self::SerializeStruct, Self::Error>; fnserialize_struct_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, len:usize, ) ->Result<Self::SerializeStructVariant, Self::Error>; // Provided methods fnserialize_i128(self, v:i128) ->Result<Self::Ok, Self::Error> { ... } fnserialize_u128(self, v:u128) ->Result<Self::Ok, Self::Error> { ... } fncollect_seq<I>(self, iter: I) ->Result<Self::Ok, Self::Error>where I:IntoIterator, <I asIntoIterator>::Item:Serialize { ... } fncollect_map<K, V, I>(self, iter: I) ->Result<Self::Ok, Self::Error>where K:Serialize, V:Serialize, I:IntoIterator<Item =(K, V)> { ... } fncollect_str<T>(self, value:&T) ->Result<Self::Ok, Self::Error>where T: ?Sized +Display { ... } fnis_human_readable(&self) ->bool { ... }}Expand description
Adata format that can serialize any data structure supported by Serde.
The role of this trait is to define the serialization half of theSerdedata model, which is a way to categorize every Rust data structure into oneof 29 possible types. Each method of theSerializer trait corresponds toone of the types of the data model.
Implementations ofSerialize map themselves into this data model byinvoking exactly one of theSerializer 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 betransient, owned, or borrowed.
- option
- Either none or some value.
- unit
- The type of
()in Rust. It represents an anonymous value containingno data.
- The type of
- unit_struct
- For example
struct UnitorPhantomData<T>. It represents a namedvalue containing no data.
- For example
- unit_variant
- For example the
E::AandE::Binenum E { A, B }.
- For example the
- newtype_struct
- For example
struct Millimeters(u8).
- For example
- newtype_variant
- For example the
E::Ninenum 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 notbe 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 thelength will be known at deserialization time without looking at theserialized data, for example
(u8,)or(String, u64, Vec<T>)or[u64; 10].
- A statically sized heterogeneous sequence of values for which thelength will be known at deserialization time without looking at theserialized 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::Tinenum 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 andwill be known at deserialization time without looking at theserialized data, for example
struct S { r: u8, g: u8, b: u8 }.
- A heterogeneous key-value pairing in which the keys are strings andwill be known at deserialization time without looking at theserialized data, for example
- struct_variant
- For example the
E::Sinenum E { S { r: u8, g: u8, b: u8 } }.
- For example the
Many Serde serializers produce text or binary data as output, for exampleJSON or Postcard. This is not a requirement of theSerializer trait, andthere are serializers that do not produce text or binary output. One exampleis theserde_json::value::Serializer (distinct from the mainserde_jsonserializer) that produces aserde_json::Value data structure in memory asoutput.
§Example implementation
Theexample data format presented on the website contains example code fora basic JSONSerializer.
Required Associated Types§
SourcetypeOk
typeOk
The output type produced by thisSerializer during successfulserialization. Most serializers that produce text or binary outputshould setOk = () and serialize into anio::Write or buffercontained within theSerializer instance. Serializers that buildin-memory data structures may be simplified by usingOk to propagatethe data structure around.
SourcetypeSerializeSeq:SerializeSeq<Ok = Self::Ok, Error = Self::Error>
typeSerializeSeq:SerializeSeq<Ok = Self::Ok, Error = Self::Error>
Type returned fromserialize_seq for serializing the content of thesequence.
SourcetypeSerializeTuple:SerializeTuple<Ok = Self::Ok, Error = Self::Error>
typeSerializeTuple:SerializeTuple<Ok = Self::Ok, Error = Self::Error>
Type returned fromserialize_tuple for serializing the content ofthe tuple.
SourcetypeSerializeTupleStruct:SerializeTupleStruct<Ok = Self::Ok, Error = Self::Error>
typeSerializeTupleStruct:SerializeTupleStruct<Ok = Self::Ok, Error = Self::Error>
Type returned fromserialize_tuple_struct for serializing thecontent of the tuple struct.
SourcetypeSerializeTupleVariant:SerializeTupleVariant<Ok = Self::Ok, Error = Self::Error>
typeSerializeTupleVariant:SerializeTupleVariant<Ok = Self::Ok, Error = Self::Error>
Type returned fromserialize_tuple_variant for serializing thecontent of the tuple variant.
SourcetypeSerializeMap:SerializeMap<Ok = Self::Ok, Error = Self::Error>
typeSerializeMap:SerializeMap<Ok = Self::Ok, Error = Self::Error>
Type returned fromserialize_map for serializing the content of themap.
SourcetypeSerializeStruct:SerializeStruct<Ok = Self::Ok, Error = Self::Error>
typeSerializeStruct:SerializeStruct<Ok = Self::Ok, Error = Self::Error>
Type returned fromserialize_struct for serializing the content ofthe struct.
SourcetypeSerializeStructVariant:SerializeStructVariant<Ok = Self::Ok, Error = Self::Error>
typeSerializeStructVariant:SerializeStructVariant<Ok = Self::Ok, Error = Self::Error>
Type returned fromserialize_struct_variant for serializing thecontent of the struct variant.
Required Methods§
Sourcefnserialize_bool(self, v:bool) ->Result<Self::Ok, Self::Error>
fnserialize_bool(self, v:bool) ->Result<Self::Ok, Self::Error>
Serialize abool value.
implSerializeforbool {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_bool(*self) }}Sourcefnserialize_i8(self, v:i8) ->Result<Self::Ok, Self::Error>
fnserialize_i8(self, v:i8) ->Result<Self::Ok, Self::Error>
Serialize ani8 value.
If the format does not differentiate betweeni8 andi64, areasonable implementation would be to cast the value toi64 andforward toserialize_i64.
implSerializefori8 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_i8(*self) }}Sourcefnserialize_i16(self, v:i16) ->Result<Self::Ok, Self::Error>
fnserialize_i16(self, v:i16) ->Result<Self::Ok, Self::Error>
Serialize ani16 value.
If the format does not differentiate betweeni16 andi64, areasonable implementation would be to cast the value toi64 andforward toserialize_i64.
implSerializefori16 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_i16(*self) }}Sourcefnserialize_i32(self, v:i32) ->Result<Self::Ok, Self::Error>
fnserialize_i32(self, v:i32) ->Result<Self::Ok, Self::Error>
Serialize ani32 value.
If the format does not differentiate betweeni32 andi64, areasonable implementation would be to cast the value toi64 andforward toserialize_i64.
implSerializefori32 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_i32(*self) }}Sourcefnserialize_i64(self, v:i64) ->Result<Self::Ok, Self::Error>
fnserialize_i64(self, v:i64) ->Result<Self::Ok, Self::Error>
Serialize ani64 value.
implSerializefori64 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_i64(*self) }}Sourcefnserialize_u8(self, v:u8) ->Result<Self::Ok, Self::Error>
fnserialize_u8(self, v:u8) ->Result<Self::Ok, Self::Error>
Serialize au8 value.
If the format does not differentiate betweenu8 andu64, areasonable implementation would be to cast the value tou64 andforward toserialize_u64.
implSerializeforu8 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_u8(*self) }}Sourcefnserialize_u16(self, v:u16) ->Result<Self::Ok, Self::Error>
fnserialize_u16(self, v:u16) ->Result<Self::Ok, Self::Error>
Serialize au16 value.
If the format does not differentiate betweenu16 andu64, areasonable implementation would be to cast the value tou64 andforward toserialize_u64.
implSerializeforu16 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_u16(*self) }}Sourcefnserialize_u32(self, v:u32) ->Result<Self::Ok, Self::Error>
fnserialize_u32(self, v:u32) ->Result<Self::Ok, Self::Error>
Serialize au32 value.
If the format does not differentiate betweenu32 andu64, areasonable implementation would be to cast the value tou64 andforward toserialize_u64.
implSerializeforu32 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_u32(*self) }}Sourcefnserialize_u64(self, v:u64) ->Result<Self::Ok, Self::Error>
fnserialize_u64(self, v:u64) ->Result<Self::Ok, Self::Error>
Serialize au64 value.
implSerializeforu64 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_u64(*self) }}Sourcefnserialize_f32(self, v:f32) ->Result<Self::Ok, Self::Error>
fnserialize_f32(self, v:f32) ->Result<Self::Ok, Self::Error>
Serialize anf32 value.
If the format does not differentiate betweenf32 andf64, areasonable implementation would be to cast the value tof64 andforward toserialize_f64.
implSerializeforf32 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_f32(*self) }}Sourcefnserialize_f64(self, v:f64) ->Result<Self::Ok, Self::Error>
fnserialize_f64(self, v:f64) ->Result<Self::Ok, Self::Error>
Serialize anf64 value.
implSerializeforf64 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_f64(*self) }}Sourcefnserialize_char(self, v:char) ->Result<Self::Ok, Self::Error>
fnserialize_char(self, v:char) ->Result<Self::Ok, Self::Error>
Serialize a character.
If the format does not support characters, it is reasonable to serializeit as a single elementstr or au32.
implSerializeforchar {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_char(*self) }}Sourcefnserialize_str(self, v: &str) ->Result<Self::Ok, Self::Error>
fnserialize_str(self, v: &str) ->Result<Self::Ok, Self::Error>
Serialize a&str.
implSerializeforstr {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_str(self) }}Sourcefnserialize_bytes(self, v: &[u8]) ->Result<Self::Ok, Self::Error>
fnserialize_bytes(self, v: &[u8]) ->Result<Self::Ok, Self::Error>
Serialize a chunk of raw byte data.
Enables serializers to serialize byte slices more compactly or moreefficiently than other types of slices. If no efficient implementationis available, a reasonable implementation would be to forward toserialize_seq. If forwarded, the implementation looks usually justlike this:
fnserialize_bytes(self, v:&[u8]) ->Result<Self::Ok,Self::Error> {letmutseq =self.serialize_seq(Some(v.len()))?;forbinv { seq.serialize_element(b)?; } seq.end()}Sourcefnserialize_none(self) ->Result<Self::Ok, Self::Error>
fnserialize_none(self) ->Result<Self::Ok, Self::Error>
Sourcefnserialize_unit(self) ->Result<Self::Ok, Self::Error>
fnserialize_unit(self) ->Result<Self::Ok, Self::Error>
Serialize a() value.
implSerializefor() {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_unit() }}Sourcefnserialize_unit_struct( self, name: &'staticstr,) ->Result<Self::Ok, Self::Error>
fnserialize_unit_struct( self, name: &'staticstr,) ->Result<Self::Ok, Self::Error>
Serialize a unit struct likestruct Unit orPhantomData<T>.
A reasonable implementation would be to forward toserialize_unit.
useserde::{Serialize, Serializer};structNothing;implSerializeforNothing {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_unit_struct("Nothing") }}Sourcefnserialize_unit_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr,) ->Result<Self::Ok, Self::Error>
fnserialize_unit_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr,) ->Result<Self::Ok, Self::Error>
Serialize a unit variant likeE::A inenum E { A, B }.
Thename is the name of the enum, thevariant_index is the index ofthis variant within the enum, and thevariant is the name of thevariant.
useserde::{Serialize, Serializer};enumE { A, B,}implSerializeforE {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {match*self{ E::A => serializer.serialize_unit_variant("E",0,"A"), E::B => serializer.serialize_unit_variant("E",1,"B"), } }}Sourcefnserialize_newtype_struct<T>( self, name: &'staticstr, value:&T,) ->Result<Self::Ok, Self::Error>
fnserialize_newtype_struct<T>( self, name: &'staticstr, value:&T,) ->Result<Self::Ok, Self::Error>
Serialize a newtype struct likestruct Millimeters(u8).
Serializers are encouraged to treat newtype structs as insignificantwrappers around the data they contain. A reasonable implementation wouldbe to forward tovalue.serialize(self).
useserde::{Serialize, Serializer};structMillimeters(u8);implSerializeforMillimeters {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_newtype_struct("Millimeters",&self.0) }}Sourcefnserialize_newtype_variant<T>( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, value:&T,) ->Result<Self::Ok, Self::Error>
fnserialize_newtype_variant<T>( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, value:&T,) ->Result<Self::Ok, Self::Error>
Serialize a newtype variant likeE::N inenum E { N(u8) }.
Thename is the name of the enum, thevariant_index is the index ofthis variant within the enum, and thevariant is the name of thevariant. Thevalue is the data contained within this newtype variant.
useserde::{Serialize, Serializer};enumE { M(String), N(u8),}implSerializeforE {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {match*self{ E::M(refs) => serializer.serialize_newtype_variant("E",0,"M", s), E::N(n) => serializer.serialize_newtype_variant("E",1,"N",&n), } }}Sourcefnserialize_seq( self, len:Option<usize>,) ->Result<Self::SerializeSeq, Self::Error>
fnserialize_seq( self, len:Option<usize>,) ->Result<Self::SerializeSeq, Self::Error>
Begin to serialize a variably sized sequence. This call must befollowed by zero or more calls toserialize_element, then a call toend.
The argument is the number of elements in the sequence, which may or maynot be computable before the sequence is iterated. Some serializers onlysupport sequences whose length is known up front.
useserde::ser::{Serialize, SerializeSeq, Serializer};impl<T> SerializeforVec<T>whereT: Serialize,{fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {letmutseq = serializer.serialize_seq(Some(self.len()))?;forelementinself{ seq.serialize_element(element)?; } seq.end() }}Sourcefnserialize_tuple( self, len:usize,) ->Result<Self::SerializeTuple, Self::Error>
fnserialize_tuple( self, len:usize,) ->Result<Self::SerializeTuple, Self::Error>
Begin to serialize a statically sized sequence whose length will beknown at deserialization time without looking at the serialized data.This call must be followed by zero or more calls toserialize_element,then a call toend.
useserde::ser::{Serialize, SerializeTuple, Serializer};impl<A, B, C> Serializefor(A, B, C)whereA: Serialize, B: Serialize, C: Serialize,{fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {letmuttup = serializer.serialize_tuple(3)?; tup.serialize_element(&self.0)?; tup.serialize_element(&self.1)?; tup.serialize_element(&self.2)?; tup.end() }}useserde::ser::{Serialize, SerializeTuple, Serializer};constVRAM_SIZE: usize =386;structVram([u16; VRAM_SIZE]);implSerializeforVram {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {letmutseq = serializer.serialize_tuple(VRAM_SIZE)?;forelementin&self.0[..] { seq.serialize_element(element)?; } seq.end() }}Sourcefnserialize_tuple_struct( self, name: &'staticstr, len:usize,) ->Result<Self::SerializeTupleStruct, Self::Error>
fnserialize_tuple_struct( self, name: &'staticstr, len:usize,) ->Result<Self::SerializeTupleStruct, Self::Error>
Begin to serialize a tuple struct likestruct Rgb(u8, u8, u8). Thiscall must be followed by zero or more calls toserialize_field, then acall toend.
Thename is the name of the tuple struct and thelen is the numberof data fields that will be serialized.
useserde::ser::{Serialize, SerializeTupleStruct, Serializer};structRgb(u8, u8, u8);implSerializeforRgb {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {letmutts = serializer.serialize_tuple_struct("Rgb",3)?; ts.serialize_field(&self.0)?; ts.serialize_field(&self.1)?; ts.serialize_field(&self.2)?; ts.end() }}Sourcefnserialize_tuple_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, len:usize,) ->Result<Self::SerializeTupleVariant, Self::Error>
fnserialize_tuple_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, len:usize,) ->Result<Self::SerializeTupleVariant, Self::Error>
Begin to serialize a tuple variant likeE::T inenum E { T(u8, u8) }. This call must be followed by zero or more calls toserialize_field, then a call toend.
Thename is the name of the enum, thevariant_index is the index ofthis variant within the enum, thevariant is the name of the variant,and thelen is the number of data fields that will be serialized.
useserde::ser::{Serialize, SerializeTupleVariant, Serializer};enumE { T(u8, u8), U(String, u32, u32),}implSerializeforE {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {match*self{ E::T(refa,refb) => {letmuttv = serializer.serialize_tuple_variant("E",0,"T",2)?; tv.serialize_field(a)?; tv.serialize_field(b)?; tv.end() } E::U(refa,refb,refc) => {letmuttv = serializer.serialize_tuple_variant("E",1,"U",3)?; tv.serialize_field(a)?; tv.serialize_field(b)?; tv.serialize_field(c)?; tv.end() } } }}Sourcefnserialize_map( self, len:Option<usize>,) ->Result<Self::SerializeMap, Self::Error>
fnserialize_map( self, len:Option<usize>,) ->Result<Self::SerializeMap, Self::Error>
Begin to serialize a map. This call must be followed by zero or morecalls toserialize_key andserialize_value, then a call toend.
The argument is the number of elements in the map, which may or may notbe computable before the map is iterated. Some serializers only supportmaps whose length is known up front.
useserde::ser::{Serialize, SerializeMap, Serializer};impl<K, V> SerializeforHashMap<K, V>whereK: Serialize, V: Serialize,{fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {letmutmap = serializer.serialize_map(Some(self.len()))?;for(k, v)inself{ map.serialize_entry(k, v)?; } map.end() }}Sourcefnserialize_struct( self, name: &'staticstr, len:usize,) ->Result<Self::SerializeStruct, Self::Error>
fnserialize_struct( self, name: &'staticstr, len:usize,) ->Result<Self::SerializeStruct, Self::Error>
Begin to serialize a struct likestruct Rgb { r: u8, g: u8, b: u8 }.This call must be followed by zero or more calls toserialize_field,then a call toend.
Thename is the name of the struct and thelen is the number ofdata fields that will be serialized.len does not include fieldswhich are skipped withSerializeStruct::skip_field.
useserde::ser::{Serialize, SerializeStruct, Serializer};structRgb { r: u8, g: u8, b: u8,}implSerializeforRgb {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {letmutrgb = serializer.serialize_struct("Rgb",3)?; rgb.serialize_field("r",&self.r)?; rgb.serialize_field("g",&self.g)?; rgb.serialize_field("b",&self.b)?; rgb.end() }}Sourcefnserialize_struct_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, len:usize,) ->Result<Self::SerializeStructVariant, Self::Error>
fnserialize_struct_variant( self, name: &'staticstr, variant_index:u32, variant: &'staticstr, len:usize,) ->Result<Self::SerializeStructVariant, Self::Error>
Begin to serialize a struct variant likeE::S inenum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls toserialize_field, then a call toend.
Thename is the name of the enum, thevariant_index is the index ofthis variant within the enum, thevariant is the name of the variant,and thelen is the number of data fields that will be serialized.len does not include fields which are skipped withSerializeStructVariant::skip_field.
useserde::ser::{Serialize, SerializeStructVariant, Serializer};enumE { S { r: u8, g: u8, b: u8 },}implSerializeforE {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {match*self{ E::S {refr,refg,refb, } => {letmutsv = serializer.serialize_struct_variant("E",0,"S",3)?; sv.serialize_field("r", r)?; sv.serialize_field("g", g)?; sv.serialize_field("b", b)?; sv.end() } } }}Provided Methods§
Sourcefnserialize_i128(self, v:i128) ->Result<Self::Ok, Self::Error>
fnserialize_i128(self, v:i128) ->Result<Self::Ok, Self::Error>
Serialize ani128 value.
implSerializefori128 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_i128(*self) }}The default behavior unconditionally returns an error.
Sourcefnserialize_u128(self, v:u128) ->Result<Self::Ok, Self::Error>
fnserialize_u128(self, v:u128) ->Result<Self::Ok, Self::Error>
Serialize au128 value.
implSerializeforu128 {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.serialize_u128(*self) }}The default behavior unconditionally returns an error.
Sourcefncollect_seq<I>(self, iter: I) ->Result<Self::Ok, Self::Error>
fncollect_seq<I>(self, iter: I) ->Result<Self::Ok, Self::Error>
Collect an iterator as a sequence.
The default implementation serializes each item yielded by the iteratorusingserialize_seq. Implementors should not need to override thismethod.
useserde::{Serialize, Serializer};structSecretlyOneHigher { data: Vec<i32>,}implSerializeforSecretlyOneHigher {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.collect_seq(self.data.iter().map(|x| x +1)) }}Sourcefncollect_map<K, V, I>(self, iter: I) ->Result<Self::Ok, Self::Error>
fncollect_map<K, V, I>(self, iter: I) ->Result<Self::Ok, Self::Error>
Collect an iterator as a map.
The default implementation serializes each pair yielded by the iteratorusingserialize_map. Implementors should not need to override thismethod.
useserde::{Serialize, Serializer};usestd::collections::BTreeSet;structMapToUnit { keys: BTreeSet<i32>,}// Serializes as a map in which the values are all unit.implSerializeforMapToUnit {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.collect_map(self.keys.iter().map(|k| (k, ()))) }}Sourcefncollect_str<T>(self, value:&T) ->Result<Self::Ok, Self::Error>
fncollect_str<T>(self, value:&T) ->Result<Self::Ok, Self::Error>
Serialize a string produced by an implementation ofDisplay.
The default implementation builds a heap-allocatedString anddelegates toserialize_str. Serializers are encouraged to provide amore efficient implementation if possible.
useserde::{Serialize, Serializer};implSerializeforDateTime {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, { serializer.collect_str(&format_args!("{:?}{:?}",self.naive_local(),self.offset())) }}Sourcefnis_human_readable(&self) ->bool
fnis_human_readable(&self) ->bool
Determine whetherSerialize implementations should serialize inhuman-readable form.
Some types have a human-readable form that may be somewhat expensive toconstruct, as well as a binary form that is compact and efficient.Generally text-based formats like JSON and YAML will prefer to use thehuman-readable one and binary formats like Postcard will prefer thecompact one.
useserde::{Serialize, Serializer};implSerializeforTimestamp {fnserialize<S>(&self, serializer: S) ->Result<S::Ok, S::Error>whereS: Serializer, {ifserializer.is_human_readable() {// Serialize to a human-readable string "2015-05-15T17:01:00Z".self.to_string().serialize(serializer) }else{// Serialize to a compact binary representation.self.seconds_since_epoch().serialize(serializer) } }}The default implementation of this method returnstrue. Data formatsmay override this tofalse to request a compact form for types thatsupport one. Note that modifying this method to change a format fromhuman-readable to compact or vice versa should be regarded as a breakingchange, as a value serialized in human-readable mode is not required todeserialize from the same data in compact mode.
Dyn Compatibility§
This trait isnotdyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<'a>Serializer for &mutFormatter<'a>
ⓘuseserde::ser::Serialize;useserde_derive::Serialize;usestd::fmt::{self, Display};#[derive(Serialize)]#[serde(rename_all ="kebab-case")]pub enumMessageType { StartRequest, EndRequest,}implDisplayforMessageType {fnfmt(&self, f:&mutfmt::Formatter) -> fmt::Result {self.serialize(f) }}
impl<'a>Serializer for &mutFormatter<'a>
useserde::ser::Serialize;useserde_derive::Serialize;usestd::fmt::{self, Display};#[derive(Serialize)]#[serde(rename_all ="kebab-case")]pub enumMessageType { StartRequest, EndRequest,}implDisplayforMessageType {fnfmt(&self, f:&mutfmt::Formatter) -> fmt::Result {self.serialize(f) }}