Search code examples
c#.netprotocol-buffersprotobuf-net

Deserialise completely unknown object with protobuf-net


I'm dealing with some protobuf encoded data for which I have no .proto definition, so none of the objects/classes are known.

The protoc compiler can produce a generic template style output for any given protobuf encoded data using protoc --decode_raw

What I'm trying to do is essentially produce a similar generic output template using probuf-net. However I'm not sure if this is possible?

So what I'd like to just deserialize the encoded data into a generic object, full of other generic objects.

Is this possible?


Solution

  • The problem here is: the protobuf format is ambiguous; without knowing the schema, there does not exist a single/reliable way to interpret the data. The exact same bytes can have many different meanings. All --decode_raw is doing is making some guesses; you can get similar guesses with protobuf-net via either the ProtoReader API, or using this handy decoder, but: you can't "simply" deserialize an object in the same way you might with JSON or XML.

    There are APIs in protobuf-net for handling unknown/unexpected fields, so sure: you could create an object where everything is unexpected, but that doesn't really help you because you still need to actually interpret each field, and you can't do that without knowledge of the schema.

    Ultimately, protobuf (the binary format, not any specific library) isn't amenable to what you want to do here.