Custom serialization

Serde code generation through #[derive(Serialize, Deserialize)] provides reasonable default serialization behavior for structs and enums and it can be customized to some extent using attributes. For unusual needs, Serde allows full customization of the serialization behavior by manually implementing Serialize and Deserialize traits for your type.

The traits each have a single method:

pub trait Serialize {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: Serializer;

pub trait Deserialize: Sized {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: Deserializer;

These methods are generic over the serialization format, represented by the Serializer and Deserializer traits. For example there is one Serializer type for JSON and a different one for Bincode.