pub trait Serialize {
    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

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()
    }
}

Implementations on Foreign Types

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.

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.

This impl requires the "rc" Cargo feature of Serde.

This impl requires the "rc" Cargo feature of Serde.

Implementors

impl<T> Serialize for Data<T> where
    T: Serialize

impl<T> Serialize for Form<T> where
    T: Serialize

impl<T: Serialize> Serialize for Json<T>

impl Serialize for CodeId

impl Serialize for Error

impl Serialize for Cause

impl Serialize for Bytes

impl Serialize for Format

impl Serialize for Health

impl Serialize for Level

impl Serialize for OpType

impl Serialize for Size

impl Serialize for Time

impl Serialize for Type

impl Serialize for Slices

impl<'a> Serialize for Country<'a>

impl<'a> Serialize for City<'a>

impl<'a> Serialize for Isp<'a>

impl<'a> Serialize for ConnectionType<'a>

impl<'a> Serialize for Domain<'a>

impl<'a> Serialize for Asn<'a>

impl<'a> Serialize for City<'a>

impl<'a> Serialize for Continent<'a>

impl<'a> Serialize for Country<'a>

impl<'a> Serialize for Location<'a>

impl<'a> Serialize for Postal<'a>

impl<'a> Serialize for RepresentedCountry<'a>

impl<'a> Serialize for Subdivision<'a>

impl Serialize for Traits

impl<'a> Serialize for SuggestResponse<'a>

impl<'a> Serialize for SuggestionWrapper<'a>

impl Serialize for Record

impl Serialize for Value

impl Serialize for Map

impl Serialize for Auth

impl Serialize for Dsn

impl<T> Serialize for Values<T> where
    T: Serialize

impl Serialize for Frame

impl Serialize for Addr

impl Serialize for RegVal

impl Serialize for Thread

impl Serialize for CError

impl Serialize for Level

impl Serialize for User

impl Serialize for SpanId

impl<'a> Serialize for Event<'a>

impl Serialize for Span

impl<'a> Serialize for Transaction<'a>

impl<'a> Serialize for SessionAttributes<'a>

impl<'a> Serialize for SessionUpdate<'a>

impl<'a> Serialize for SessionAggregates<'a>

impl Serialize for Value

impl Serialize for Number

impl<'a, T, U> Serialize for SerializeAsWrap<'a, T, U> where
    T: ?Sized,
    U: ?Sized,
    U: SerializeAs<T>, 

impl Serialize for Value

impl<T: Serialize> Serialize for Spanned<T>

impl<'a> Serialize for SerializeFieldMap<'a, Event<'_>>

impl<'a> Serialize for SerializeFieldMap<'a, Attributes<'_>>

impl<'a> Serialize for SerializeFieldMap<'a, Record<'_>>

impl<'a> Serialize for SerializeFieldSet<'a>

impl<'a> Serialize for SerializeLevel<'a>

impl<'a> Serialize for SerializeId<'a>

impl<'a> Serialize for SerializeMetadata<'a>

impl<'a> Serialize for SerializeEvent<'a>

impl<'a> Serialize for SerializeRecord<'a>

impl<S> Serialize for Host<S> where
    S: Serialize

impl Serialize for Url

impl Serialize for Uuid

impl Serialize for Simple

impl Serialize for Urn

impl Serialize for Braced