I'm using proto3 with grpc, and I'm looking at a more efficient way of creating a protobuf message without using a builder and populating it.
If I have a string (from Message.toString()), can I recreate the message with the string?
If I have a message type
message TestMessage {
bool status = 1;
uint64 created = 2;
TestNested submessage = 3;
message TestNested {
int32 messageNumber = 1;
Entry entry = 2;
}
message Entry {
int32 id = 1;
EntryType entryType = 2;
}
enum EntryType {
DEFAULT_ENTRY = 0;
OTHER_ENTRY = 1;
}
}
and the below output:
status: true
created: 1534240073415
submessage {
messageNumber: 3
entry{
id: 47957
entryType: DEFAULT_ENTRY
}
}
How can I create the TestMessage? I've looked at the JavaDoc to see if there's a parseFrom()
method that takes in a string, but I'm not winning.
I was looking for the TextFormat parser. The format that Message.toString()
prints is the TextFormat representation.
To convert back to a protobuf message, I did the below:
TestMessage testMessage = new TestMessage.newBuilder();
CharSequence myText = "status: true\n ...";
com.google.protobuf.TextFormat.getParser().merge(myText, testMessage);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With