Google protobuf at run-time deserialization example in C++

Lately I have been looking at Google’s format for interchanging data between multiple processes or Protocol Buffers. As you would expect for an inter-process data format Protocol Buffers are supported on different platforms and implementations are available for most popular programming languages. An interesting example of such a platform is the stm32l1 micro controller, which is supported by a third party protobuf implementation named nano pb.

One of the interesting feats of Google’s C++ protobuf implementation is that it supports deserializing arbitrary protobuf objects at run-time. As long as the proto definition is known at run time, the library is capable of parsing any message back into the individual fields from the proto definition. This is especially useful in situations where a program has to be able to handle proto definitions that are unknown at compile time. In this case the standard approach of using the proto compiler (protoc) to construct C++ class definitions, writing (de)serialization code and linking the resulting program with these definitions is obviously not possible.

This post addresses the lack of examples that demonstrate this ‘at run-time deserialization’ feature of Google’s C++ protobuf implementation. The example below was constructed by combining the protobuf unit tests with this example provided by . It has been tested with the v3.0.0-beta-3 from Google’s github repo. Use at your own discretion. The example should be self explanatory.
Continue reading “Google protobuf at run-time deserialization example in C++”