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 Serialize for SocketAddr
Available on crate feature std
only.
impl Serialize for SocketAddr
std
only.source§impl Serialize for SocketAddrV4
Available on crate feature std
only.
impl Serialize for SocketAddrV4
std
only.source§impl Serialize for SocketAddrV6
Available on crate feature std
only.
impl Serialize for SocketAddrV6
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 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<'a, T> Serialize for Cow<'a, T>where
T: Serialize + ToOwned + ?Sized,
Available on crate features std
or alloc
only.
impl<'a, T> Serialize for Cow<'a, T>where T: Serialize + ToOwned + ?Sized,
std
or alloc
only.source§impl<Idx> Serialize for RangeInclusive<Idx>where
Idx: Serialize,
impl<Idx> Serialize for RangeInclusive<Idx>where Idx: Serialize,
source§impl<K, V> Serialize for BTreeMap<K, V>where
K: Serialize,
V: Serialize,
Available on crate features std
or alloc
only.
impl<K, V> Serialize for BTreeMap<K, V>where K: Serialize, V: Serialize,
std
or alloc
only.source§impl<K, V, H> Serialize for HashMap<K, V, H>where
K: Serialize,
V: Serialize,
Available on crate feature std
only.
impl<K, V, H> Serialize for HashMap<K, V, H>where K: Serialize, V: Serialize,
std
only.source§impl<T0, T1, T2> Serialize for (T0, T1, T2)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
impl<T0, T1, T2> Serialize for (T0, T1, T2)where T0: Serialize, T1: Serialize, T2: Serialize,
source§impl<T0, T1, T2, T3> Serialize for (T0, T1, T2, T3)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
impl<T0, T1, T2, T3> Serialize for (T0, T1, T2, T3)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize,
source§impl<T0, T1, T2, T3, T4> Serialize for (T0, T1, T2, T3, T4)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
impl<T0, T1, T2, T3, T4> Serialize for (T0, T1, T2, T3, T4)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize,
source§impl<T0, T1, T2, T3, T4, T5> Serialize for (T0, T1, T2, T3, T4, T5)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
impl<T0, T1, T2, T3, T4, T5> Serialize for (T0, T1, T2, T3, T4, T5)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize,
source§impl<T0, T1, T2, T3, T4, T5, T6> Serialize for (T0, T1, T2, T3, T4, T5, T6)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
impl<T0, T1, T2, T3, T4, T5, T6> Serialize for (T0, T1, T2, T3, T4, T5, T6)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize,
source§impl<T0, T1, T2, T3, T4, T5, T6, T7> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
impl<T0, T1, T2, T3, T4, T5, T6, T7> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize,
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize,
source§impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
T9: Serialize,
impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> Serialize for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize, T9: Serialize,
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)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
T9: Serialize,
T10: Serialize,
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)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize, T9: Serialize, T10: Serialize,
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)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
T9: Serialize,
T10: Serialize,
T11: Serialize,
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)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize, T9: Serialize, T10: Serialize, T11: Serialize,
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)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
T9: Serialize,
T10: Serialize,
T11: Serialize,
T12: Serialize,
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)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize, T9: Serialize, T10: Serialize, T11: Serialize, T12: Serialize,
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)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
T9: Serialize,
T10: Serialize,
T11: Serialize,
T12: Serialize,
T13: Serialize,
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)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize, T9: Serialize, T10: Serialize, T11: Serialize, T12: Serialize, T13: Serialize,
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)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
T9: Serialize,
T10: Serialize,
T11: Serialize,
T12: Serialize,
T13: Serialize,
T14: Serialize,
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)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize, T9: Serialize, T10: Serialize, T11: Serialize, T12: Serialize, T13: Serialize, T14: Serialize,
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)where
T0: Serialize,
T1: Serialize,
T2: Serialize,
T3: Serialize,
T4: Serialize,
T5: Serialize,
T6: Serialize,
T7: Serialize,
T8: Serialize,
T9: Serialize,
T10: Serialize,
T11: Serialize,
T12: Serialize,
T13: Serialize,
T14: Serialize,
T15: Serialize,
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)where T0: Serialize, T1: Serialize, T2: Serialize, T3: Serialize, T4: Serialize, T5: Serialize, T6: Serialize, T7: Serialize, T8: Serialize, T9: Serialize, T10: Serialize, T11: Serialize, T12: Serialize, T13: Serialize, T14: Serialize, T15: Serialize,
source§impl<T> Serialize for Box<T>where
T: Serialize + ?Sized,
Available on crate features std
or alloc
only.
impl<T> Serialize for Box<T>where T: Serialize + ?Sized,
std
or alloc
only.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>where
T: Serialize + ?Sized,
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for Rc<T>where T: Serialize + ?Sized,
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>where
T: Serialize + ?Sized,
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for RcWeak<T>where T: Serialize + ?Sized,
rc
and (crate features std
or alloc
) only.This impl requires the "rc"
Cargo feature of Serde.
source§impl<T> Serialize for Arc<T>where
T: Serialize + ?Sized,
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for Arc<T>where T: Serialize + ?Sized,
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>where
T: Serialize + ?Sized,
Available on crate feature rc
and (crate features std
or alloc
) only.
impl<T> Serialize for ArcWeak<T>where T: Serialize + ?Sized,
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 Mutex<T>where
T: Serialize + ?Sized,
Available on crate feature std
only.
impl<T> Serialize for Mutex<T>where T: Serialize + ?Sized,
std
only.source§impl<T> Serialize for RwLock<T>where
T: Serialize + ?Sized,
Available on crate feature std
only.
impl<T> Serialize for RwLock<T>where T: Serialize + ?Sized,
std
only.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.