How to map java.lang.Object in .proto file [Protobuf]

我是研究僧i 提交于 2019-12-12 01:25:17

问题


How can I map java.lang.Object in .proto file. I'd like to have smth like:

message User {
 string name = 1;
 Object field = 2;
}

回答1:


Disregarding the strangeness of your requirement for a moment: serialize your Java object to byte[] (formally implementing Serializable, or any way you want) and use protobuf value type bytes.

But this is really not the way to use protobuf. It's meant to transfer data, not objects. What of your object you would like to transfer over the wire? You cannot serialize methods and nontrivial object dependencies anyway. Only value types and simple structures (arrays, maps). So declare a proto message which reflects the data structure of your object, and serialize to that instead of byte[].



来源:https://stackoverflow.com/questions/41878400/how-to-map-java-lang-object-in-proto-file-protobuf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!