Converting a Protocol Buffer to a POJO

2019-02-19 01:41发布

问题:

Is there a tool or library that can map a Protobuff object to a POJO. I would like to have the pojo implement other interfaces that I can not with the ProtoBuff object. Or would I have to manually do this conversion?

回答1:

Maybe too late, but you can try protobuf-converter library. It is easy to use.

Convert POJO object to related protobuf message:

POJOClass pojoInstance = new POJOClass();
...
ProtobufClass protoInstance = Converter.create().toProtobuf(ProtobufClass.class, pojoInstance );

Code for backward conversion:

POJOClass pojoInstance = Converter.create().toDomain(POJOClass.class, protoInstance);


回答2:

take a look at ProtoStuff. I think it will do what you want.

I believe that you will want to use the "java-bean" compiler.

Edit: Changed the link.