Question

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:

  • Does protocol buffer know built-in Java-defined class such as BigDecimal, BigInteger,…?
  • If we have a complex class which recursively has other user-defined classes as members, do we need to travel all classes’ definition and declare them as message which their filed in .proto file? If so, It is very terrible for user (and maybe they use some external java library and do not have source code of defined classes in that library)

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top