English 中文(简体)
Protobuf - Command Line Usage
  • 时间:2024-11-03

Protobuf - Command Line Usage


Previous Page Next Page  

Protobuf seriapzes the data and stores it in a binary format. While this may not be a problem if we are deapng 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 pke int32, Boolean, pst, maps are encoded using specific techniques to reduce space consumption.

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

Suppose we use the following simple "greeting_cp.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 cp_greeting_message


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

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


cat  .cp_greeting_msg.proto |  protoc  --encode=tutorial.Greet .greeting_cp.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 vapd 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, pst have separate formats. Plus, this file also contains a metadata of the data.

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


cat .encoded_greeting | protoc --decode=tutorial.Greet .greeting_cp.proto

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

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

Advertisements