Raj Hassani
Raj Hassani

Reputation: 1667

How to use protoc for decoding protobuf files

I am so confused with the usage of protoc and cannot find examples on the internet regarding its usage:-

protoc -IPATH=path_to_proto_file_directory  path_to_proto_file --decode=MESSAGE_TYPE  < ./request.protobuf 

So what is the message_type here and can someone write a full correct example of it

Upvotes: 5

Views: 6441

Answers (2)

DieOde
DieOde

Reputation: 151

Several years later, here is another answer.

# Check the version
protoc --version
>libprotoc 3.0.0

Raw Decoding

You can use the --decode_raw option without a schema (.proto file). Consider this simple example message containing the bytes 0x08 0x01. I am assuming a Linux environment with echo

# Use echo to print raw bytes and don't print an extra `\n` newline char at the end
echo -en '\x08\x01' | protoc --decode_raw

# Output below. This means field 1 integer type with a value of 1
1: 1

Decode With A Schema (.proto file)

If you have the proto file then you can get better results than --decode_raw. You could even send the output from --decode back into protoc if you wanted to encode it with new values.

Example.proto file contents
syntax = "proto3";

message Test {
  int32 FieldOneNumber
}
# Decode the same message as the raw example against this schema
echo -en '\x08\x1' | protoc --decode="Test" --proto_path= ./Example.proto

# Output. Note that the generic field named 1 has been replaced by the proto file name of FieldOne.
FieldOne: 1

Upvotes: 0

Иван Дуюн
Иван Дуюн

Reputation: 121

syntax = "proto3";
package response;

// protoc --gofast_out=. response.proto

message Response {
  int64 UID        
  ....
}

use protoc:
protoc --decode=response.Response response.proto < response.bin
protoc --decode=[package].[Message type] proto.file < protobuf.response

or use: 
protoc --decode_raw < protobuf.response
without proto file.

Upvotes: 2

Related Questions