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()
}
}
Dyn Compatibility§
This trait is not dyn 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 Serialize for SocketAddr
impl Serialize for SocketAddr
Source§impl Serialize for SocketAddrV4
impl Serialize for SocketAddrV4
Source§impl Serialize for SocketAddrV6
impl Serialize for SocketAddrV6
Source§impl Serialize for AtomicBool
Available on crate feature std
and target_has_atomic="8"
only.
impl Serialize for AtomicBool
std
and target_has_atomic="8"
only.Source§impl Serialize for AtomicIsize
Available on crate feature std
and target_has_atomic="ptr"
only.
impl Serialize for AtomicIsize
std
and target_has_atomic="ptr"
only.Source§impl Serialize for AtomicUsize
Available on crate feature std
and target_has_atomic="ptr"
only.
impl Serialize for AtomicUsize
std
and target_has_atomic="ptr"
only.Source§impl Serialize for SystemTime
Available on crate feature std
only.
impl Serialize for SystemTime
std
only.Source§impl Serialize for NonZeroI16
impl Serialize for NonZeroI16
Source§impl Serialize for NonZeroI32
impl Serialize for NonZeroI32
Source§impl Serialize for NonZeroI64
impl Serialize for NonZeroI64
Source§impl Serialize for NonZeroI128
impl Serialize for NonZeroI128
Source§impl Serialize for NonZeroIsize
impl Serialize for NonZeroIsize
Source§impl Serialize for NonZeroU16
impl Serialize for NonZeroU16
Source§impl Serialize for NonZeroU32
impl Serialize for NonZeroU32
Source§impl Serialize for NonZeroU64
impl Serialize for NonZeroU64
Source§impl Serialize for NonZeroU128
impl Serialize for NonZeroU128
Source§impl Serialize for NonZeroUsize
impl Serialize for NonZeroUsize
Source§impl<Idx> Serialize for RangeInclusive<Idx>where
Idx: Serialize,
impl<Idx> Serialize for RangeInclusive<Idx>where
Idx: Serialize,
Source§impl<T> Serialize for (T₁, T₂, …, Tₙ)where
T: Serialize,
impl<T> Serialize for (T₁, T₂, …, Tₙ)where
T: Serialize,
This trait is implemented for tuples up to 16 items long.
Source§impl<T> Serialize for BinaryHeap<T>where
T: Serialize,
Available on crate features std
or alloc
only.
impl<T> Serialize for BinaryHeap<T>where
T: Serialize,
std
or alloc
only.Source§impl<T> Serialize for BTreeSet<T>where
T: Serialize,
Available on crate features std
or alloc
only.
impl<T> Serialize for BTreeSet<T>where
T: Serialize,
std
or alloc
only.Source§impl<T> Serialize for LinkedList<T>where
T: Serialize,
Available on crate features std
or alloc
only.
impl<T> Serialize for LinkedList<T>where
T: Serialize,
std
or alloc
only.Source§impl<T> Serialize for VecDeque<T>where
T: Serialize,
Available on crate features std
or alloc
only.
impl<T> Serialize for VecDeque<T>where
T: Serialize,
std
or alloc
only.Source§impl<T> Serialize for Rc<T>
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for Rc<T>
rc
and (crate features std
or alloc
) only.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.
Source§impl<T> Serialize for RcWeak<T>
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for RcWeak<T>
rc
and (crate features std
or alloc
) only.This impl requires the "rc"
Cargo feature of Serde.
Source§impl<T> Serialize for Arc<T>
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for Arc<T>
rc
and (crate features std
or alloc
) only.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.
Source§impl<T> Serialize for ArcWeak<T>
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for ArcWeak<T>
rc
and (crate features std
or alloc
) only.This impl requires the "rc"
Cargo feature of Serde.
Source§impl<T> Serialize for Vec<T>where
T: Serialize,
Available on crate features std
or alloc
only.
impl<T> Serialize for Vec<T>where
T: Serialize,
std
or alloc
only.Source§impl<T> Serialize for PhantomData<T>where
T: ?Sized,
impl<T> Serialize for PhantomData<T>where
T: ?Sized,
Source§impl<T> Serialize for Saturating<T>where
T: Serialize,
impl<T> Serialize for Saturating<T>where
T: Serialize,
Source§impl<T, H> Serialize for HashSet<T, H>where
T: Serialize,
Available on crate feature std
only.
impl<T, H> Serialize for HashSet<T, H>where
T: Serialize,
std
only.