Parse Google Protocol Buffers datagram without .pr

2019-02-13 21:48发布

问题:

is it possible to parse an incoming google protocol buffers datagram without any .proto file? I merely now its been serialized using protocol buffers but have no idea about the IDL file.

I'm looking for a way to just iterate through any value by some sort of reflection? Is this possible?

Thank you!

回答1:

protoc --decode_raw < my_file

You need to take the following things into account when inspecting the output:

  • None of the field names are visible, just the tag numbers.
  • All varint-fields are shown as integers. This is ok for most types, but sint* will appear in the "zigzagged" format.
  • Doubles and floats will be shown as hex.
  • Bytes, string fields and submessages all appear the same, i.e. just a bunch of bytes.

If you want to decode the messages programmatically, you can write your own .proto file after you have figured out what the fields mean using the above method.