Parse PB bytes into a generic message

957 views
Skip to first unread message

Jimm

unread,
Oct 27, 2010, 11:34:53 AM10/27/10
to Protocol Buffers
Hello,
I need a generic parser in java, which can de-serialize PB bytes
buffer into a Message or GeneratedMessage. But, GeneratedMessage
provides no such method to merge an arbitary PB bytes and provide an
instance of itself.
Any leads would be greatly appreciated.

Adam Vartanian

unread,
Oct 27, 2010, 2:14:11 PM10/27/10
to Jimm, Protocol Buffers
> I need a generic parser in java, which can de-serialize PB bytes
> buffer into a Message or GeneratedMessage. But, GeneratedMessage
> provides no such method to merge an arbitary PB bytes and provide an
> instance  of itself.

From just a message's bytes you can't turn it into a Message. The
serialized format doesn't include type information.

You've got a couple choices. First, if you can send the message's
Descriptor along with the message, then you can use
DynamicMessage.parseFrom(Descriptor, byte[]) to deserialize it. If
you can't send the message's Descriptor along, then you can use
UnknownFieldSet.parseFrom(byte[]), but that's not going to give you
very much at all, just tag numbers, their wiretype, and their value.

- Adam

maninder batth

unread,
Oct 27, 2010, 3:34:24 PM10/27/10
to Protocol Buffers
Since the client and server are remote, i am not sure how i can send
Descriptor along with message?

Adam Vartanian

unread,
Oct 27, 2010, 4:00:06 PM10/27/10
to maninder batth, Protocol Buffers
> Since the client and server are remote, i am not sure how i can send
> Descriptor along with message?

All the different kinds of descriptors have associated protocol
messages available through their toProto() methods, so you can
serialize them, send them over the wire, and deserialize them on the
other end. You have to deserialize a FileDescriptorProto, though, so
you can either send the appropriate FileDescriptorProto or just send a
DescriptorProto and build the file on the receiving end.

- Adam

Reply all
Reply to author
Forward
0 new messages