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:
- It is a much more compact alternative to XML with significantly reduced processing time.
- It provides a way to store and exchange structured data between software, possibly written in different programming languages, as well as between the server and the client.
Feature library is enabled to help you use it in web services. - It allows language-independent classes to be serialized. Serialisaton creates compact and fast binary code for processing.
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
- Object-oriented language, each message inherits the Message class.
- Typical data language.
- Text and binary formats.
- The Protoc compiler generates a class in the desired language based on the data definition.
- Protoc provides C++ or Java classes and is designed to be compatible with all languages.
- The class is serialized in a binary file. The thread can also create a binary file directly from the PB language.
- The unit is called a "message." A.proto file can contain multiple messages.
- Supports namespaces.
- The message structure is recursive, the proto-structure may have elements of other proto-structures .
- A repeating field, as in XML, can be reused in the same message.
- Dynamically extensible.
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...
- Textbook on the Buffers protocol. First use.
- JSON. Simpler format for JavaScript and server languages.
- FlatBuffers. Also Google, a very fast serialization framework that allows you to use stored data without loading a file into memory. Usage example, data used by the game as maps, sprites, etc.