Explaining protobuf And Why I Don’t Use It Very Often

Protocol Buffer, also known as protobuf, is an interesting alternative to XML or JSON or C-family language classes or structs. Briefly, you can define what is very much like a class using a special (but very simple) language. Then you use a special compiler to generate code in C++ or Java or C# or Python. The generated code can be used like normal, and any object generated by protobuf has a built-in WriteTo() method that can serialize the object to binary and send it over a network in a very efficient way.

Note: To keep the main ideas of protobuf clear, I skip some details and I use some minor syntax shortcuts.

Suppose you wanted to create a C# class to represent a person. Such code would look something like:

public class DateOfBirth {
  public int year;
  public int month;
  public int day;

public class Phone {
  public string number;
  public string kind; // like "cell" or "work"

public class Person {
  public string first;
  public string last;
  public DateOfBirth dob;
  public Phone[] phones;

Person p1 = new Person();
p1.first = "John";
p1.last = "Smith";
// etc., etc.
// plus custom code to send a Person object over a network like:
// mySender.Send(p1);

If you wanted to use protobuf, you’d define a Person “message” something like this:

message DateOfBirth {
  required int32 year = 1;
  required int32 month = 2;
  required int32 year = 3;

message Phone {
  required string number = 1;
  optional string kind = 2;

message Person {
  required string first = 1;
  required string last = 2;
  optional DateOfBirth dob = 3;
  repeated Phone phones = 4;

Then you’d save this as person.proto and then use the protobuf compiler to generate C# code, something like this:

protoc --proto_path="C:/myprotos/" --csharp_out

If you wanted Java, C++, or Python code, you’d compile using the –java_out, –cpp_out, or –python_out switch.

With the C# switch, you’d get a file Person.cs that you could include to a C# program and use something like this:

Person p1 = new Person();
p1.first = John";
p1.last = "Smith";
// etc.
// set up a C# FileStream object

Therefore, protobuf is useful for scenarios where you have some sort of system that you want to be useable by different languages, or useful for systems where you want a high-performance way to serialize and send data in a (binary) format.

I rarely find myself in either scenario. When I code a program I almost always target a single programming language and single OS platform. And when I need to serialize and send data over a network, I rarely send vast amounts of data, so I can just use the much simpler and human-readable JSON text format, or if I have to, I’ll use XML. But I think it’s very cool to be able to get three different languages generated from one common background file.

I came across these three paintings by an unknown (to me) artist. They reminded me of protobuf — notice that the desk and chair are the same background but the artist generated three different paintings from the common background. I like these — not sure what they are — they all have a vaguely neo Italian Renaissance look to me, and the models have a vaguely Mona Lisa mystery look.

This entry was posted in Miscellaneous. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s