What's a better way of encoding data if you don't want to use JSON, or XML .

What is Protocol Buffers? 

 Short answer: It's a way to serialize unstructured data. 


- Smaller
- 20-100 x faster
- Easier
- 3-10 x smaller then xml
- Generates code for you
- You don't have to handwrite your own parsing code

You define (.proto file) the structure of your data then there is code generated to read and write to data streams. Protocol buffer was developed at Google to help with their index server.  Some developers even use protobuf for storing data persistently for example BigTable. Some developers even use it for RPC ( Remote Procedure Call ) systems. See overview for more on this:


Here is an example of a .proto file:

package tutorial;
option java_package = "com.learning.protobuf"
option java_outer_classname = "SomeOuterClassIMadeUp"
message MyTopSecretMsg {
     required String info = 1 ;
     . . .

Note: The 'required' keyword could be replaced by 'optional'.
See protobuf language guide for more details on how to define your .proto files:

When you define your .proto file then you can compile it.

What is it used for?

Used as a communication protocol etc. 

How do I install it?

 $ tar -zxvf protoc**.tar.gz ; 
 $ configure;                         
 $ make ;                               
 $ make install ;                    

How do I compile the .proto file ?

protoc -I=$PWD --java_out=$PLACE_TO_PUT_THE_GENERATED_CODE addressbook.proto

NOTE: $PWD means this present working directory


Popular Posts