Trait serde::ser::Serialize[][src]

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<Block: BlockType> Serialize for BitVec<Block> where
    Block: Serialize

impl<T, N> Serialize for GenericArray<T, N> where
    T: Serialize,
    N: ArrayLength<T>, 

impl Serialize for PublicKey

impl Serialize for Bytes

impl Serialize for ByteBuf

impl Serialize for UpgradeableLoaderState

impl Serialize for Clock

impl Serialize for EpochSchedule

impl Serialize for Feature

impl Serialize for FeeCalculator

impl Serialize for FeeRateGovernor

impl Serialize for Hash

impl Serialize for InstructionError

impl Serialize for Instruction

impl Serialize for AccountMeta

impl Serialize for CompiledInstruction

impl Serialize for Hash

impl Serialize for LoaderInstruction

impl Serialize for UpgradeableLoaderInstruction

impl Serialize for MessageHeader

impl Serialize for Message

impl Serialize for Data

impl Serialize for State

impl Serialize for Versions

impl Serialize for ProgramError

impl Serialize for PubkeyError

impl Serialize for Pubkey

impl Serialize for ParsePubkeyError

impl Serialize for Rent

impl Serialize for ShortU16

impl<T: Serialize> Serialize for ShortVec<T>

impl Serialize for SlotHashes

impl Serialize for SlotHistory

impl Serialize for Config

impl Serialize for StakeInstruction

impl Serialize for LockupArgs

impl Serialize for LockupCheckedArgs

impl Serialize for AuthorizeWithSeedArgs

impl Serialize for AuthorizeCheckedWithSeedArgs

impl Serialize for StakeState

impl Serialize for StakeAuthorize

impl Serialize for Lockup

impl Serialize for Authorized

impl Serialize for Meta

impl Serialize for Delegation

impl Serialize for Stake

impl Serialize for StakeHistoryEntry

impl Serialize for StakeHistory

impl Serialize for SystemError

impl Serialize for SystemInstruction

impl Serialize for Fees

impl Serialize for Entry

impl Serialize for RecentBlockhashes

impl Serialize for Rewards