语言特定数据结构的 protobuf 序列化

发布于 2024-07-08 15:37:22 字数 335 浏览 5 评论 0原文

使用谷歌的协议缓冲区,我有一个已经用Java编写的服务,它有自己的数据结构已经。 我想使用 pb 来传递消息,并且我正在寻找一种方法将 Java 中的现有数据结构序列化为 pb。 我可以从头开始定义 pb 中的所有数据结构,这可能是正确的方法,但我太懒了。 那么,假设我有一个 Java(或其他支持的语言)中的 Person 类或一个包含数十个属性的 Plane 类,有没有办法将该类序列化为 pb? 我可以拥有 Plane 类型的 pb 属性吗? (当Plane不是pb时,它是一个Java类)

Using google's Protocul Buffers, I have a service already written in Java which has its own data structures already. I'd like to use pb to delivering messages and I'm looking for a way to serialize the existing data structures that I have in Java to pb.
I can start by defining all the data structures in pb from scratch, which is probably the right way to go but I'm too lazy.
So, say I have a Person class in Java (or other supported languages) or a Plane class which has tens of attributes in it, is there a way to serialize that class to pb? Can I have a pb attribute of type Plane? (when Plane is not a pb, it's a Java class)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水中月 2024-07-15 15:37:22

不,你不能。 protobuf 消息中的字段始终是原语(基本上是数字、字符串和字节数组)、protobuf 枚举(作为 Java 枚举生成)或 protobuf 消息 - 当然,还有所有这些的重复版本。

您可能会编写一个工具,使用反射从 Java 类创建 .proto 文件,但我怀疑您会发现手动完成会更快。 特别是,如果您确实使用了反射,您需要确保字段始终以相同的名称生成,以保持兼容性。 您可以做的一件事是注释 Java 类并编写代码以根据这些注释生成 .proto 文件 - 甚至可能使用注释直接序列化为 proto 格式。 就我个人而言,我建议以某种方式创建 .proto 文件,而不是有效地重写 PB 项目 - 否则,在已经经过彻底测试的代码中存在引入错误的重大风险。

如果您确实创建了一个注释系统,我确信 Kenton Varda(以及 PB 社区的其他成员)会有兴趣看到它。

No, you can't. Fields in protobuf messages are always the primitives (numbers, strings and byte arrays, basically), protobuf enums (which are generated as Java enums) or protobuf messages - and repeated versions of all of those, of course.

You could potentially write a tool which used reflection to create a .proto file from a Java class, but I suspect you'd find it quicker just to do it by hand. In particular, if you did use reflection you'd want to make sure that the fields were always generated with the same name, to maintain compatibility. One thing you could do is annotate the Java classes and write code to generate the .proto file based on those annotations - or even potentially serialize directly to proto format using the annotations. Personally I'd recommend creating the .proto file in some way rather than effectively rewriting the PB project - otherwise there's a significant risk of introducing bugs where there's already thoroughly tested code.

If you do create an annotation system, I'm sure Kenton Varda (and the rest of the PB community) would be interested in seeing it.

此刻的回忆 2024-07-15 15:37:22

我能想到的一种方法是在 protobuf 中有一个字符串字段,并使用 Java 的原始序列化将 Java 类序列化到该字段。 这样,假设消息的接收者知道如何读取/反序列化它,我可以轻松地将 Java 序列化为 Java 消息。

不过,这种技术也有缺点。 仅举几例:

  1. 它只是 Java 到 Java(没有 C++、Python 或其他)
  2. 它不如本机 protobuf 高效(无论是解析/序列化还是消息大小)
  3. 您的数据结构逻辑分散在多个地方,一些在 protobufs 定义文件中,一些在其他 Java 类中,这使得维护变得更加困难。

但是——它可以在短期内完成工作。

One way I can think of is to have a string field in a protobuf and serialize a Java class to that field using Java's primitive serialization. That way, assuming the receiver of the message knows how to read/deserialize it, I can easily serialize Java to Java messages.

There are downsides to this technique, though. To name a few:

  1. It's only Java to Java (no C++, Python or others)
  2. It's not as efficient as native protobufs are (neither parsing/serializatin wise nor message size wise)
  3. You have the logic of the data structures scattered around in several places, some are in the protobufs definition file, some in other Java classes and this makes things harder to maintain.

But - it gets the job done for the short term.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文