1
votes

All I have is raw protobuf binary data, I don't have access to .proto file, and I need to convert it to json string in Java. So is there is a way to do that in java? i.e. something similar to protoc tool

2
What have you tried so far - SamHoque
to be honest, my information about protobuf is limited. I tried several things I found on different tutorials on internet, but I couldn't find any easy way to directly convert say byte array to json. like what protoc --decode_raw is doing - Anthony J.
I don't think you can decode the protobuf without having the .proto schema. You need to know which fields it contains. - marstran
nopes, you can't. - Mangat Rai Modi
@AnthonyJ. In such case, take a look here: developers.google.com/protocol-buffers/docs/reference/java/com/…. This implements the majority of the logic from the Python answer you mentioned before. If that answer is correct, you will need to switch on the last 3 bits of the field tag to learn the type. However, be aware of the limitations of this approach as listed here: stackoverflow.com/a/7343928/2946480. After all, protobuf is a schemaless data format. - Michał

2 Answers

3
votes

This won't be possible without the .proto schema. For multiple reasons:

  • the raw binary doesn't include field names, just numbers; you can of course create JSON with integer-looking properties, but:
  • the data format is ambiguous without a schema:
    • "varint" (simple integers) can mean multiple different things in different contexts, including signed, unsigned, zig-zag signed, or Boolean
    • ditto for fixed length, which could be integers (signed or unsigned) or floating-point
    • "length prefix" could be a utf-8 string, a packed array, or a sub-message

So: there is simply no good and reliable way of understanding data without the schema, let alone choosing how to display it as JSON.

0
votes

.proto file is basically schema of your proto buffer.

protoc.exe tool is used for generating the files (i.e. used for creating the proto buffer by providing setter and getters methods.)

yes their is available some methods which convert the protobuffer into JSON like i am using protobuffer in c++ and it provide some methods to convert it into JSON.

same also will be available in Java as well. so don't mix protoc tool concept with this 'data format conversion'

try to use this api provided by Protobuffer offical

https://developers.google.com/protocol-buffers/docs/reference/java/

https://developers.google.com/protocol-buffers/docs/reference/java/

Hope it will help