Show all elements in a protocol buffer message

2019-07-24 21:04发布

How can I show all elements in a protocol buffer message? Do I need to use reflection or convert the message into an XML message and then show it? Ideally some generic code that will work for any message. Lars

1条回答
我命由我不由天
2楼-- · 2019-07-24 22:02

A protobuf message is internally ambiguous unless you have the .proto schema (or can infer a schema) available, as (for example) a "string" wire-type could represent:

  • a utf-8 string
  • a BLOB
  • a sub-message
  • a packed array

Similar ambiguity exists for all wire-types (except perhaps "groups").

My recommendation would be to run it through your existing deserialization process (against the type-model that you presumably have available in the project) to get an object model suitable for inspection. From the object-model you have all the usual options - reflection, serialization via XmlSerializer / JavaScriptSerializer, etc.

If all you have is the raw data, there is a wireshark plugin that might help, or protobuf-net exists a ProtoReader class that might be useful for parsing such a stream; but the emphasis here is that the stream is tricky to decipher in isolation.

查看更多
登录 后发表回答