Protocol Buffers, for data exchange with a server beyond JSON

Protocol Buffers is a data definition language created by Google that can be compared to IDL, but is much simpler. Its syntax, based on the C language, evokes that of JSON, with the difference of the use of typed variables.
Google has defined this language for use on its own servers that store and exchange big quantities of structured data, and in 2008 decided to make it open source. It is used in Android to speed up exchanges with the server (in Marketplace for example).

The proto files have a dual format, the human readable source and the binary that can be handled quickly by the machine.
It may be used for three reasons among other:

A simple format with advanced tools

First, some definitions to see more clearly:

Protocol Buffers: name of the language and name of units of data encapsulated into a proto file.
Proto: a data definition file in the PB language, with the .proto extension.
Protoc: name of the compiler that produces classes or binaries.

Features of the language


Each source has the form:

message name     {     
  ...list of data fields...   

The main scalar types are string, int32, int64, float, double, bool.
Variables may be declared with a modifier: required, optional, repeated.

A sequence number is assigned to each variable, which is a directive to the compiler and not a value for the variable.

required string x = 1     // 1 is not a value

An initial value may be assigned with the default directive:

required string x = 1 [default="Some text"];    

In addition to primitives, nested types are added by embedding a message into another message:

message container
  required int32 number = 1;  
  message contained
    repeated string x = 1;

The "contained" object and its variables can be accessed through a string as container.contained.x

Enumerations with the type enum can be included in messages.

When we defined the structure of a message, it is used in a program by creating an instance. To it are associated methods specific to the class and produced by the compiler int C++ or Java generated files.

container myinstance;

Sample code

Simple message.

message hello
    required string = 1 [default="the message"];
    optional int32 = 2;

See the definition of the PB programming language for more details.

Download the Protoc compiler and get the full documentation included in the archive.

See also...