Protobuf - Command Line Usage



Protobuf serializes the data and stores it in a binary format. While this may not be a problem if we are dealing simply with strings, because ultimately Protobuf uses UTF-8. So, any text that it stores would be human readable if you are using a UTF8 enabled reader. However, things like int32, Boolean, list, maps are encoded using specific techniques to reduce space consumption.

That is why, at times, encoding/decoding a message via simple command line utility is useful for testing purposes. Let us see this in action −

Suppose we use the following simple "greeting_cli.proto"

syntax = "proto3";
package tutorial;
option java_package = "com.tutorialspoint.greeting";

message Greet {
   string greeting = 1;
   string username = 2;
   int32 age = 3;
}  

And we create a message in cli_greeting_message

greeting: "Yo"
username : "John"
age : 50

Now, let us encode this message using Protobuf CLI tool −

cat  .\cli_greeting_msg.proto |  protoc  --encode=tutorial.Greet .\greeting_cli.proto > encoded_greeting

If we look at what is inside this file or cat this file −

cat .\encoded_greeting

☻Yo↕♦John↑2

You will notice some weird characters apart from "Yo" and "John". That is because these encoding may not be a valid unicode/UTF-8 encoding. UTF-8 is what is used, generally speaking, at most of the places. And this is used for string in case of Protobuf, but ints, maps, Boolean, list have separate formats. Plus, this file also contains a metadata of the data.

That is why, we need a decoder/deserializer to read this data. Let us use that.

cat .\encoded_greeting | protoc --decode=tutorial.Greet .\greeting_cli.proto

greeting: "Yo"
username : "John"
age : 50

So, as we see, we are able to get the data back which was serialized and looked weird in the file.

Advertisements