Richard Le
Richard Le

Reputation: 669

Generate .proto file for complex Java object

I want to serialize a complex Java object throw Internet. First time, I used Google Gson to serialize the class. Gson offers an easy way to serialize object into JSON string and deserialize from JSON string to object via toJson and fromJson. However, JSON string is not so compact and introduce large overhead when serialize byte[] array.

I am reading Google Protocol Buffer. According to the tutorial, Users have to write .proto file for each message manually. It seems to that Protocol Buffer could not recognize user-defined-class (Google protocol buffers - user defined java objects as messages fields). I have 2 questions:

Is there a tool to automatically traverse the Java class and generate .proto?

Upvotes: 5

Views: 11183

Answers (1)

xosp7tom
xosp7tom

Reputation: 2183

There are a list of possible types in protobuf. BigDecimal/BigInteger are not. You can write your own conversion step tho. https://developers.google.com/protocol-buffers/docs/proto

Yes, you need to travel and write mapping from/to java class and proto. It is a necessary step and everyone has to do unfortunately. I don't think there is an automated tool for that. Maybe you can write one and open-source it? :)

I think, what you ask is complete one-to-one mapping from java class to protobuf message. Generally, such mapping is very hard to be done in automated fashion. Also, Google purposely limits protobuf as one serialization protocol, not ORM like hibernate and spring.

Upvotes: 3

Related Questions