Using the FlatBuffers library

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-called getter and setter methods to access the data fields. The getters perform deserialization on the fly. Storing data in its serialized form makes FlatBuffers really memory efficient. There is no need for extra memory to store serialized data and, in most cases, the overhead of deserialization is low.

In this recipe, we will learn how to start using FlatBuffers for data serialization in our applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.186.164