Protocol Buffers an Alternate to JSON

cover-photo

What are Protocol Buffers

Protocol buffers also known as Protobuf are an efficient and flexible mechanism for serializing structured data. Protobuf works in a way where you define the data to be structured once and then with a specially generated source code you can read and write your structured data. Protocol buffers are language-neutral and platform-neutral, at a time when this post is written they have support for Java, Python, Objective-C, and C++, also with the new proto3 language version, you can also use them with Dart, Go, Ruby, and C#, with more languages to come.

How do Protocol Buffers work

We need to specify how we want the information that we are serializing to be structured by defining Protobuf message types in .proto files. Each Protobuf message is small information that contains a series of name-value pairs. Let's see a basic example of a proto file how looks like.

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

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

As we can see the message format is very simple, each message type has one or more numbered fields, where each field has a name and a value type. The value types can be booleans, integer, floating-point, strings, raw bytes, or other protocol buffer message types, allowing you to structure your data hierarchically. When you finish defining the messages, you run the protocol buffer compiler for the particular application language that you are using on your proto file to generate data access classes. This will provide getters and setters for each field also methods to serialize/parse the structure to and from raw bytes.

Note: Protocol Buffer classes are basically just data holders, they don't make good first class citizens in an object model. If you want to add richer behaviour to a generated class, the best way to do this is to wrap the protocol buffer class in an application-specific class.

Protocol Buffers vs XML

Protobuf has many advantages over XML, let's see some of the pros.

  • simpler
  • 3 to 10 times smaller
  • 20 to 100 times faster
  • less ambiguous
  • generate data access classes that are easier to use programmatically

If we do a simple example in XML will look like:

<person>
  <name>John Doe</name>
  <email>jdoe@example.com</email>
</person>

while the Protobuf message (protocol buffer text format) looks like:

# Textual representation of a protocol buffer.
# This is *not* the binary format used on the wire.
person {
  name: "John Doe"
  email: "jdoe@example.com"
}

When the above message is encoded to the Protobuf binary code format is 28 bytes long and will take around 100-200 nanoseconds to parse. For comparison, the XML version is at least 69 bytes and would take around 5,000-10,000 nanoseconds to parse. Manipulating protocol buffer is also easier compared to XML. Let's see them both in the examples below.

cout << "Name: " << person.name() << endl;
cout << "E-mail: " << person.email() << endl;

Protobuf manipulation

cout << "Name: "
      << person.getElementsByTagName("name")->item(0)->innerText()
      << endl;
cout << "E-mail: "
      << person.getElementsByTagName("email")->item(0)->innerText()
      << endl;

XML manipulation

Protobuf vs JSON

As we know JSON is a subset of the JavaScript programming language. Its messages are exchanged in human-readable text format and are completely independent and supported by almost all programming languages. On the other hand protocol buffers are not only a message format. It is a set of rules and tools that define and exchange messages. Currently is restricted to only some programming languages. Also, it has more data types than JSON, like methods, enumerates, and has other functions, including RPC (Remote Procedure Calls).

Advantages of Protobuf:

  • Smaller and faster
  • RPC support
  • Structure validation

Advantages of JSON:

  • Human readability
  • More resources and support
  • Bigger community

Conclusion

As Protocol buffers are a great modern method for serializing structured data they also offer some nice advantages over JSON and XML, while it is still not a full JSON replacement, protocol buffers offer its greatest advantages in terms of speed, encoding and decoding size of the data.




#protocolbuffers #protobuf #json #xml

Author: Aleksandar Vasilevski |

Loading...