Transfer protocol buffer object to POJO java object

5,030 views
Skip to first unread message

Sean Nguyen

unread,
Dec 28, 2010, 4:55:18 PM12/28/10
to Protocol Buffers
Hi,

I have a protocol buffer object like:

message PBPerson
{
optional string lastName = 1;
optional string firstName = 2;
}

I also have another java object

class JavaPerson
{
private String lastName;
private String firstName;

// setter and getter
}

I want to convert from PBPerson to JavaPerson and vice versa and I
don't want to do it manually by writing getter and setter for each
fields because my object can have more than 10 fields. Is there a
utility from protocol buffer that helps me doing that. So the would
expect a utility class that does something like:

JavaPerson javaPerson = PBConverter.convert(PBPerson pbPerson);

PBPerson pbPerson = PBConverter.convert(JavaPerson javaPerson);

Thanks,

Kenton Varda

unread,
Dec 28, 2010, 6:16:23 PM12/28/10
to Sean Nguyen, Protocol Buffers
I don't know of any existing tools for this.  You could write code that does this via reflection (protobuf reflection on the protobuf object, and basic java reflection on the POJO).  Or, you could write a protoc plugin which generates the code you need, though that will be a lot more complicated.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Sean Nguyen

unread,
Dec 29, 2010, 9:11:51 AM12/29/10
to Protocol Buffers
I am writing a utility base on your suggestion and I run into a
problem of getting a camel name out of a FieldDescriptor.

My proto message is like this:

message Person
{

optional string last_name = 1;
optional string first_name = 2;

}


When java object is generated it uses camel field name as lastName,
firstName. Is there a way to get the camel field name out? I don't
want to get the original field name name : last_name, first_name and
convert it again to camel field name every time that I want to do
getter and setter on java pojo (very inefficient).


Thanks,


On Dec 28, 5:16 pm, Kenton Varda <ken...@google.com> wrote:
> I don't know of any existing tools for this.  You could write code that does
> this via reflection (protobuf reflection on the protobuf object, and basic
> java reflection on the POJO).  Or, you could write a protoc plugin which
> generates the code you need, though that will be a lot more complicated.
>
> On Tue, Dec 28, 2010 at 3:55 PM, Sean Nguyen <sontran...@gmail.com> wrote:
> > Hi,
>
> > I have a protocol buffer object like:
>
> > message PBPerson
> > {
> > optional string lastName = 1;
> > optional string firstName = 2;
> > }
>
> > I also have another java object
>
> > class JavaPerson
> > {
> > private String lastName;
> > private String firstName;
>
> > // setter and getter
> > }
>
> > I want to convert from PBPerson to JavaPerson and vice versa and I
> > don't want to do it manually by writing getter and setter for each
> > fields because my object can have more than 10 fields. Is there a
> > utility from protocol buffer that helps me doing that. So the would
> > expect a utility class that does something like:
>
> > JavaPerson javaPerson = PBConverter.convert(PBPerson pbPerson);
>
> > PBPerson pbPerson = PBConverter.convert(JavaPerson javaPerson);
>
> > Thanks,
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Protocol Buffers" group.
> > To post to this group, send email to prot...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > protobuf+u...@googlegroups.com<protobuf%2Bunsu...@googlegroups.com>
> > .

Kenton Varda

unread,
Dec 29, 2010, 4:05:55 PM12/29/10
to Sean Nguyen, Protocol Buffers
No, the camel-case name is not stored anywhere.  You will need to construct it yourself.  If performance is a concern, just cache the results in a Map<FieldDescriptor, String>.

To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.

users ....

unread,
Dec 29, 2010, 4:30:13 PM12/29/10
to Protocol Buffers
One other thing to consider (if performance is not an issue) is to
extend Smooks (www.smooks.org). I have been considering this my self
although currently I do not have enough of a need to write this yet.
Basically you would write the Smooks "cartridge" to read a PB. You
can throw SAX events in compliance with their current architecture
dynamically thanks to PB having the capability of being self
describing. Smooks performance is not bad (better than most XML to
Java parsing), but its not as fast as JIBX or manual code. If you want
to translate back from POJO to PB you'll also have to implement
"visitors" to do that work. All in all it does not appear too
complicated, and you would get the additional benefits of smooks (such
as XML, EDI, Java binding) for free.

-Mike

On Dec 29, 4:05 pm, Kenton Varda <ken...@google.com> wrote:
> No, the camel-case name is not stored anywhere.  You will need to construct
> it yourself.  If performance is a concern, just cache the results in a
> Map<FieldDescriptor, String>.
>
> > <protobuf%2Bunsu...@googlegroups.com<protobuf%252Buns...@googlegroups.com>

chris.nz

unread,
Dec 30, 2010, 6:56:44 AM12/30/10
to Protocol Buffers


On Dec 28, 9:55 pm, Sean Nguyen <sontran...@gmail.com> wrote:
> I want to convert from PBPerson to JavaPerson and vice versa and I
> don't want to do it manually by writing getter and setter for each
> fields because my object can have more than 10 fields. Is there a
> utility from protocol buffer that helps me doing that. So the would
> expect a utility class that does something like:
>
> JavaPerson javaPerson = PBConverter.convert(PBPerson pbPerson);
>
> PBPerson pbPerson = PBConverter.convert(JavaPerson javaPerson);

I'm after the same thing. I suggest you take a look at protostuff:
http://code.google.com/p/protostuff/

I only just discovered it myself a few days ago and seems to do what
you want (actually it converts to/from POJO to protobuf byte[]). It
also appears to be very efficient - often faster than protocol buffers
itself: http://code.google.com/p/thrift-protobuf-compare/wiki/BenchmarkingV2

There are a few caveats but on the whole it looks rather promising.
The conversion process is straightforward:

// Generate a schema based on your Pojo
Schema<Pojo> schema = RuntimeSchema.createFrom(Pojo.class);

// Convert the Pojo to a byte[]
Pojo p = ...
LinkedBuffer buf = LinkedBuffer.allocate(500);
byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf);

// Populate a Pojo based on a byte[] and schema
Pojo p2 = new Pojo();
ProtobufIOUtil.mergeFrom(data, p2, schema);
Reply all
Reply to author
Forward
0 new messages