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

Implementors

impl<T> Serialize for BinaryTreeNode<T>where
    T: Serialize,

impl<T> Serialize for BinaryTree<T>where
    T: Serialize,

impl Serialize for Loss

impl Serialize for Config

impl Serialize for Data

impl Serialize for GBDT

impl Serialize for Value

impl Serialize for Number

impl Serialize for Result

impl Serialize for Entry

impl Serialize for User

impl Serialize for UserID

impl<T: Clone> Serialize for TaskFiles<T>where
    T: Serialize,

impl<S: StateTag> Serialize for Task<S>where
    S: Serialize,

impl Serialize for Create

impl Serialize for Assign

impl Serialize for Stage

impl Serialize for Run

impl Serialize for Finish

impl Serialize for Done

impl Serialize for Cancel

impl Serialize for Fail

impl Serialize for Value

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

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

impl Serialize for Url

impl Serialize for Uuid