- Protobuf - Discussion
- Protobuf - Useful Resources
- Protobuf - Quick Guide
- Protobuf - In Other Languages
- Protobuf - Integration with Kafka
- Protobuf - Rules to Update Definition
- Protobuf - Command Line Usage
- Protobuf - Compound Data Types
- Protobuf - Language Independence
- Protobuf - Optionality & Defaults
- Protobuf - Nested Class
- Protobuf - Map
- Protobuf - List/Repeated
- Protobuf - Enum
- Protobuf - Boolean
- Protobuf - Numbers
- Protobuf - Strings
- Protobuf - Class/Member
- Protobuf - Constructs
- Protobuf - Basic App
- Protobuf - Introduction
- Protobuf - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Protobuf - Command Line Usage
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