Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Serialization

Serialization is the process of turning structured data into a flat format, usually a textual or binary format. Typically this is done to save data (on disk, in a database), exchange it (between processes, between services). Deserialization is the process of doing the inverse: turning a flat representation into a structured representation.

For example:

  • When you read a config file from disk and parse it, you are deserializing it.
  • When you make an API request and send JSON-encoded data, you are serializing it.

Performing serialization and deserialization is important for any program that must communicate with the outside world.

Rust has some popular crates that are used for doing this. The most popular crate is serde (which stands for serialize, deserialize). Many Rust crates have optional feature flags that make their types compatible with it.

NameDescription
serdeGeneral purpose serialization and deserialization library.
miniserdeSerialization and deserialization library that is designed to be similar to serde, while doing less monomorphization and thereby producing smaller code.
bincodeBinary serialization library, designed for inter-process communication. It is compatible with serde, but also has the option of using its own traits for more control of the layout.
facetReflection library that is able to use the information about types to serialize and deserialize them.

Serde

Versioned Structs

Default Values

Renaming Fields

Custom Implementations

Bincode

Facet

Miniserde

Reading

Serde by David Tolnay

The serde book is a reference guide for how to use serde, lists the various formats that serde can serialize and deserialize, and gives advice on using advanced features.

In this article, Andre goes through several serialization frameworks in Rust and explains which ones are stable and reliable and fit for use in production Rust applications.