Protocol buffers for data exchange outside of JSON

Protocol Buffers is a data definition language created by Google that can be compared to IDL, but which is much simpler. Its syntax, based on that of the C language, refers to JSON syntax with distinction from the use of typical variables.
Google identified this language for use on its own servers, which store and exchange volumes of structured data, and in 2008 decided to move it to an open source. It is used in Android to speed up the exchange with the server (for example, in the Marketplace).

Proto files have a dual format, a human-readable source, and a binary file that can be quickly processed by the machine.
It can be used for three reasons:

Simple format with advanced tools

First, some definitions to clarify:

Protocol Buffers - the name of the language and the name of the data units encapsulated in the protos files.
Proto: PB data definition file and .proto extension.
Protoc is the name of the compiler that generates the classes or binary.

Language features

Syntax

Each source has the form:

message nom     {     
  ...liste de champs de données...   
 }

The main types of scalar data are string, int32, int64, float, double, bool.
Variables can be declared together with the required, optional, repeating modifier.

They are assigned a sequence number, which is a compiler directive, not a variable value.

required string x = 1  // ce n'est pas la valeur 1

The start value is assigned to the default directive:

required string x = 1 [default="Un texte"];    

Nested types are added to primitives, defined by including a message in another message:

message contenant
{
  required int32 numero = 1;  
  message contenu
  {
    repeated string x = 1;
  }
}

The content object and its variables can be accessed by: contant.contenu.x

You can include enumerations with an enumeration type in messages.

When you define the structure of a message, the program uses it by creating an instance. And we link methods specific to the message and created by the source compiler proto.

contenant moninstance;
moninstance.set_numero(18);

Sample code

Show Message.

message hello
{
    required string = 1 [default="le message"];
    optional int32 = 2;
} 

Learn more about the format in the PB language definition.

Download the Protoc compiler with complete documentation .

See also...