pub trait Serialize {
    fn serialize<S>(
        &self,
        serializer: S
    ) -> Result<<S as Serializer>::Ok, <S as Serializer>::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§

Serializes an account view key into string or bytes.

Serializes an account compute key into bytes.

Serializes the signature into string or bytes.

Serializes an account private key into string or bytes.

Serializes an account graph key into bytes.

Serializes an account address into a string or as bytes.

Serializes the boolean into a string or as bytes.

Serializes the field into a string or as bytes.

Serializes the group into a string or as bytes.

Serializes the scalar into a string or as bytes.

Serializes the integer into a string or as bytes.

Serializes the string into bytes if the format is not human-readable.

Serializes the record type into string or bytes.

Serializes the identifier into string or bytes.

Serializes the input ID into string or bytes.

Serializes the state path into string or bytes.

Serializes the locator into string or bytes.

Serializes the record ciphertext into a string or as bytes.

Serializes the struct into string or bytes.

Serializes the record plaintext into a string or as bytes.

Serializes the literal type into string or bytes.

Serializes the leaf into string or bytes.

Serializes the literal into a string or as bytes.

Serializes the program ID into string or bytes.

Serializes the value into string or bytes.

Serializes the register into string or bytes.

Serializes the leaf into string or bytes.

Serializes the ciphertext into string or bytes.

Serializes the value type into string or bytes.

Serializes the request into string or bytes.

Serializes the finalize type into string or bytes.

Serializes the entry type into string or bytes.

Serializes the leaf into string or bytes.

Serializes the plaintext into a string or as bytes.

Serializes the register type into string or bytes.

Serializes the plaintext type into string or bytes.

Implementors§