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<A> Serialize for ContractLink<A> where
    A: Serialize

impl<A> Serialize for Callback<A> where
    A: Serialize

impl<A> Serialize for ContractStatus<A> where
    A: Serialize

impl<P: Permission> Serialize for Permit<P> where
    P: Serialize

impl<P: Permission> Serialize for PermitParams<P> where
    P: Serialize

impl Serialize for PubKey

impl<P: Permission> Serialize for SignedPermit<P> where
    P: Serialize

impl Serialize for Fee

impl Serialize for Coin

impl<P: Permission> Serialize for PermitMsg<P> where
    P: Serialize

impl<P: Permission> Serialize for PermitContent<P> where
    P: Serialize

impl Serialize for Tx

impl Serialize for RichTx

impl Serialize for Schema

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

impl Serialize for Coin

impl Serialize for Binary

impl<T> Serialize for CosmosMsg<T> where
    T: Clone + Debug + PartialEq + JsonSchema,
    T: Serialize

impl Serialize for GovMsg

impl<T> Serialize for InitResponse<T> where
    T: Clone + Debug + PartialEq + JsonSchema,
    T: Serialize

impl<T> Serialize for HandleResponse<T> where
    T: Clone + Debug + PartialEq + JsonSchema,
    T: Serialize

impl<T> Serialize for MigrateResponse<T> where
    T: Clone + Debug + PartialEq + JsonSchema,
    T: Serialize

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

impl Serialize for Env

impl Serialize for Empty

impl<Ser: Serde, T: Serialize> Serialize for Base64Of<Ser, T>

impl Serialize for Tx

impl Serialize for RichTx

impl Serialize for Mint

impl Serialize for Burn

impl Serialize for Send

impl Serialize for Trait

impl Serialize for Tx

impl Serialize for Status

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

impl Serialize for Value

impl Serialize for Number