pub trait Serialize {
// Required method
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer;
}
Expand description
A data structure that can be serialized into any data format supported by Serde.
Serde provides Serialize
implementations for many Rust primitive and
standard library types. The complete list is here. All of
these can be serialized using Serde out of the box.
Additionally, Serde provides a procedural macro called serde_derive
to
automatically generate Serialize
implementations for structs and enums in
your program. See the derive section of the manual for how to use this.
In rare cases it may be necessary to implement Serialize
manually for some
type in your program. See the Implementing Serialize
section of the
manual for more about this.
Third-party crates may provide Serialize
implementations for types that
they expose. For example the linked-hash-map
crate provides a
LinkedHashMap<K, V>
type that is serializable by Serde because the crate
provides an implementation of Serialize
for it.
Required Methods§
sourcefn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize this value into the given Serde serializer.
See the Implementing Serialize
section of the manual for more
information about how to implement this method.
use serde::ser::{Serialize, SerializeStruct, Serializer};
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
// This is what #[derive(Serialize)] would generate.
impl Serialize for Person {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("Person", 3)?;
s.serialize_field("name", &self.name)?;
s.serialize_field("age", &self.age)?;
s.serialize_field("phones", &self.phones)?;
s.end()
}
}
Object Safety§
Implementations on Foreign Types§
source§impl<T0, T1, T2> Serialize for (T0, T1, T2)
impl<T0, T1, T2> Serialize for (T0, T1, T2)
source§impl<T0, T1, T2, T3> Serialize for (T0, T1, T2, T3)
impl<T0, T1, T2, T3> Serialize for (T0, T1, T2, T3)
source§impl<T0, T1, T2, T3, T4> Serialize for (T0, T1, T2, T3, T4)
impl<T0, T1, T2, T3, T4> Serialize for (T0, T1, T2, T3, T4)
source§impl<T0, T1, T2, T3, T4, T5> Serialize for (T0, T1, T2, T3, T4, T5)
impl<T0, T1, T2, T3, T4, T5> Serialize for (T0, T1, T2, T3, T4, T5)
source§impl<T0, T1, T2, T3, T4, T5, T6> Serialize for (T0, T1, T2, T3, T4, T5, T6)
impl<T0, T1, T2, T3, T4, T5, T6> Serialize for (T0, T1, T2, T3, T4, T5, T6)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7)
impl<T0, T1, T2, T3, T4, T5, T6, T7> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)
Implementors§
impl Serialize for IpAddr
impl Serialize for SocketAddr
impl Serialize for Ipv4Addr
impl Serialize for Ipv6Addr
impl Serialize for SocketAddrV4
impl Serialize for SocketAddrV6
impl Serialize for AtomicBool
impl Serialize for AtomicI8
impl Serialize for AtomicI16
impl Serialize for AtomicI32
impl Serialize for AtomicI64
impl Serialize for AtomicIsize
impl Serialize for AtomicU8
impl Serialize for AtomicU16
impl Serialize for AtomicU32
impl Serialize for AtomicU64
impl Serialize for AtomicUsize
impl Serialize for CStr
impl Serialize for CString
impl Serialize for Duration
impl Serialize for OsStr
impl Serialize for OsString
impl Serialize for Path
impl Serialize for PathBuf
impl Serialize for String
impl Serialize for SystemTime
impl Serialize for NonZeroI8
impl Serialize for NonZeroI16
impl Serialize for NonZeroI32
impl Serialize for NonZeroI64
impl Serialize for NonZeroI128
impl Serialize for NonZeroIsize
impl Serialize for NonZeroU8
impl Serialize for NonZeroU16
impl Serialize for NonZeroU32
impl Serialize for NonZeroU64
impl Serialize for NonZeroU128
impl Serialize for NonZeroUsize
impl<'a> Serialize for Arguments<'a>
impl<'a, T> Serialize for Cow<'a, T>
impl<Idx> Serialize for Range<Idx>where
Idx: Serialize,
impl<Idx> Serialize for RangeFrom<Idx>where
Idx: Serialize,
impl<Idx> Serialize for RangeInclusive<Idx>where
Idx: Serialize,
impl<Idx> Serialize for RangeTo<Idx>where
Idx: Serialize,
impl<K, V> Serialize for BTreeMap<K, V>
impl<K, V, H> Serialize for HashMap<K, V, H>
impl<T> Serialize for Bound<T>where
T: Serialize,
impl<T> Serialize for Option<T>where
T: Serialize,
impl<T> Serialize for Arc<T>
This impl requires the "rc"
Cargo feature of Serde.
Serializing a data structure containing Arc
will serialize a copy of
the contents of the Arc
each time the Arc
is referenced within the
data structure. Serialization will not attempt to deduplicate these
repeated data.
impl<T> Serialize for ArcWeak<T>
This impl requires the "rc"
Cargo feature of Serde.
impl<T> Serialize for BTreeSet<T>where
T: Serialize,
impl<T> Serialize for BinaryHeap<T>where
T: Serialize,
impl<T> Serialize for Box<T>
impl<T> Serialize for Cell<T>
impl<T> Serialize for LinkedList<T>where
T: Serialize,
impl<T> Serialize for Mutex<T>
impl<T> Serialize for PhantomData<T>where
T: ?Sized,
impl<T> Serialize for Rc<T>
This impl requires the "rc"
Cargo feature of Serde.
Serializing a data structure containing Rc
will serialize a copy of
the contents of the Rc
each time the Rc
is referenced within the
data structure. Serialization will not attempt to deduplicate these
repeated data.
impl<T> Serialize for RcWeak<T>
This impl requires the "rc"
Cargo feature of Serde.