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:
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.