开发者

Show all elements in a protocol buffer message

开发者 https://www.devze.com 2023-03-13 05:34 出处:网络
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?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消