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