Santhosh Tpixler
Santhosh Tpixler

Reputation: 371

protocolbuffer de-serialization without the class name in Java

I have a serialized proto in a file(sample.dat). I was provided with the jar file which contains a lot of proto compiled files which are again compiled into .class files. I don't know the type or class of the proto in the sample.dat. How can I de-serialize it? Is there any generic way?

Upvotes: 2

Views: 499

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062925

How many proto descriptor types are we talking about in the jar file? Basically: can you look at each descriptor type and see if it works, or looks about right?

Basically, unless you're using the Any feature, protobuf data makes no mention of what any object's type is - you ideally just need to know. It is, however, quite easy to parse the raw format to look at what you're getting; you can do this with the protoc tool using the --decode_raw option, or online via tools like https://protogen.marcgravell.com/decode. Either of this will show you what it can about your data, hopefully allowing you to figure out which type it looks like, based on comparing the structure you see to one of:

  • the message definitions in a .proto file
  • the generated types in source code
  • the generated types in a library

Note: binary protobuf data does not include member names - just field numbers. If you can see the generated source, or attribute metadata, or decompile a compiled library to look at what the serialize/deserialize code looks like, you can manually map names to numbers by inspection - to figure out whether a type looks likely.

Note: if the java code contains descriptor binary for the types, the compiled descriptor data can also be decoded - each should represent a DescriptorProto from descriptor.proto. This won't help tell you what the root type is, but might help you understand each type, since it provides full access to the metadata.

Upvotes: 3

Related Questions