Click here to Skip to main content
13,900,697 members
Click here to Skip to main content
Add your own
alternative version

Stats

1.4K views
1 bookmarked
Posted 14 Mar 2019
Licenced CPOL

Protocol Buffers: or How to Serialize Data

, 14 Mar 2019
Rate this:
Please Sign up or sign in to vote.
Protocol Buffers: or how to serialize data

This post is meant as a brief introduction that will point you in the right direction rather than an exhaustive tutorial. Here we go…

Have you ever had to write code that serialized structured data into an efficient binary format, to be later saved to disk or sent over the network? Do you remember how difficult and time consuming it was? Haven’t you wished there was a standard C++ library to do it instead of reinventing the wheel? Well, today is your lucky day. 🙂

Say hello to Google’s Protocol Buffers! It is a highly portable (and free!) library that allows you to define near arbitrary (numerical, strings, structures, lists, vectors, maps, you name it) data formats and easily serialize and deserialize them into a platform portable binary format. Why not use XML you may ask? How about 3 to 10 times smaller output, and 20 to 100 times faster serialization and deserialization just to name a few. 🙂

Let’s start by defining a protocol buffer file that defines a “Person” data structure:

syntax = "proto2";
  
package data;

message Person
{
  required string name = 1;
  required int32 dob = 2;
  optional string email = 3;
}

Save that to protobuf.proto file and let’s compile it using the protoc code generator; yes, Protocol Buffers is a code generator; it takes as input a .proto file and spits out C++ classes (it can also produce code in C#, Java, JS, ObjC, PHP, Python, and Ruby).

protoc basic usage:

protoc -I=. –cpp_out=. protobuf.proto

The above command will produce 2 files: protobuf.pb.h and protobuf.pb.cc. Let’s now write a simple program that creates, serializes, and deserializes our data structure using the generated classes:

#include <iostream>
#include <string>
#include "protobuf.pb.h"
using namespace std;

int main(int argc, char** argv)
{
        GOOGLE_PROTOBUF_VERIFY_VERSION;

        data::Person p;

        p.set_name("Martin Vorbrodt");
        p.set_dob(19800830);
        p.set_email("martin@vorbrodt.blog");

        string binary;
        p.SerializeToString(&binary);

        data::Person p2;
        p2.ParseFromString(binary);

        cout << "Name  = " << p2.name() << endl;
        cout << "DOB   = " << p2.dob() << endl;
        cout << "EMail = " << p2.email() << endl;
        
        google::protobuf::ShutdownProtobufLibrary();
        return 1;
}

This code is pretty self explanatory. We create, set and serialize one data structure of type data::Person, then deserialize it into another. The output is what we would expect.

Program output:

Name  = Martin Vorbrodt
DOB   = 19800830
EMail = martin@vorbrodt.blog

That’s enough of an introduction. I hope you will read up on Protocol Buffers and realize the enormous potential of this library. Happy coding!

P.S. As always, the complete source and build files are available at my GitHub.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Martin Vorbrodt
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 15 Mar 2019
Article Copyright 2019 by Martin Vorbrodt
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid