如何在 .proto 文件中映射 java.lang.Object [Protobuf] [英] How to map java.lang.Object in .proto file [Protobuf]

查看:467
本文介绍了如何在 .proto 文件中映射 java.lang.Object [Protobuf]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 .proto 文件中映射 java.lang.Object.我想要像:

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;
}

推荐答案

暂时忽略你的奇怪需求:将你的 Java 对象序列化为 byte[](正式实现 Serializable,或任何你想要)并使用 protobuf 值类型 bytes.

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.

但这确实不是使用protobuf的方式.它旨在传输数据,而不是对象.您想通过电汇传输什么对象?无论如何,您不能序列化方法和重要的对象依赖项.只有值类型和简单结构(数组、映射).因此,声明一个反映对象数据结构的 proto 消息,并将其序列化为该消息而不是 byte[].

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[].

这篇关于如何在 .proto 文件中映射 java.lang.Object [Protobuf]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆