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);

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

Reply via email to