Serialization and deserialization using Serde
Serde is the de-facto standard way of serializing and deserializing data in Rust. Serde supports a number of data structures that it can serialize out of the box to a number of given data formats (including JSON, and TOML, CSV). The easiest way to understand Serde is to think of it as an invertible function that transforms a given data structure into a stream of bytes. Other than standard data types, Serde also provides a few macros that can be implemented on user defined data types, making them (de)serializable.
In Chapter 2, Introduction to Rust and its Ecosystem, we discussed how procedural macros can be used to implement custom derives for given data types. Serde uses that mechanism to provide two custom derives, named Serialize
and Deserialize
, that can be implemented for user-defined data types that are composed of data types that Serde supports. Let us look at a small example of how this works. We start with creating the empty project using...