Serialization and deserialization is a complex topic. While ad hoc serialization may look simple and straightforward, it is difficult to make it generic, easy to use, and fast. Thankfully, there are libraries that handle all of these complexities.
In this recipe, we will learn how to use one of the serialization libraries: FlatBuffers. It was designed with embedded programming in mind, making serialization and deserialization memory efficient and fast.
FlatBuffers uses an Interface Definition Language (IDL) to define a data schema. The schema describes all the fields of data structures that we need to serialize. When the schema is designed, we use a special tool called flatc to generate the code for a particular programming language, which is C++ in our case.
The generated code stores all data in serialized form and provides developers with so...