问题
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