Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can protocol buffer parse a message in "debug string" format?

I'd like to use protocol buffer in my program to read data from a file. I also would like to be able to edit the data file with any text editor, for a start (I'll write a data editor later on, and switch to full binary).

Is there a way to parse a human-readable format ? (debug string provided by protobuf itself, or some other format).

like image 565
Gnurfos Avatar asked Aug 14 '11 11:08

Gnurfos


3 Answers

The question doesn't specify the programming language, and my answer is only about Java.

In Java, a Message instance's toString method returns a human-readable textual format. The same format can then be parsed into a Message instance by TextFormat.merge:

String messageString = ...
MyMessage.Builder builder = MyMessage.newBuilder();
TextFormat.merge(messageString, builder);
MyMessage newMessage = builder.build();

(Variations of the merge method can also read from a stream, to avoid reading the whole message string into memory.)

like image 188
Eli Acherkan Avatar answered Sep 25 '22 00:09

Eli Acherkan


There is a text based format too, but support for this is implementation specific. For example, I don't support it at all in protobuf-net. But yes: such is defined, and discussed (for example) here: http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.text_format.html

Personally, I'd rather use binary and write a UI around the model.

like image 30
Marc Gravell Avatar answered Sep 26 '22 00:09

Marc Gravell


If you don't mind using command-line tools, the Piqi project includes piqi convert command for converting between 4 formats: binary Protocol Buffers, JSON, XML and Piq. The Piq format is specially designed for viewing and editing data in a text editor.

like image 23
alavrik Avatar answered Sep 25 '22 00:09

alavrik