JSON 和 Protocol Buffers 之间是否有标准映射?

发布于 2024-08-27 06:31:55 字数 645 浏览 6 评论 0原文

来自对公告博文的评论:

关于 JSON:JSON 是结构化的 与协议缓冲区类似,但是 协议缓冲区二进制格式仍然是 更小且编码速度更快。 JSON 为以下内容提供了出色的文本编码 不过,协议缓冲区——它是 编写编码器/解码器很简单 转换任意协议 与 JSON 之间的消息,使用 protobuf 反射。这是一个很好的 与 AJAX 应用程序通信的方式, 因为让用户下载完整的 protobuf 解码器当他们访问你的 页面可能太多。

制作一个映射可能很简单,但是两者之间是否存在任何两个独立的开发团队自然会选择的单一“明显”映射?如果两个产品支持 PB 数据并且可以互操作,因为它们共享相同的 .proto 规范,我想知道如果它们独立引入相同规范的 JSON 反射,它们是否仍然能够互操作。可能需要做出一些任意的决定,例如枚举值应该由字符串(以人类可读的典型 JSON)表示还是由它们的整数值表示?

那么是否有一个既定的映射,以及用于从 .proto 规范生成 JSON 编码器/解码器的开源实现?

From a comment on the announcement blog post:

Regarding JSON: JSON is structured
similarly to Protocol Buffers, but
protocol buffer binary format is still
smaller and faster to encode. JSON
makes a great text encoding for
protocol buffers, though -- it's
trivial to write an encoder/decoder
that converts arbitrary protocol
messages to and from JSON, using
protobuf reflection. This is a good
way to communicate with AJAX apps,
since making the user download a full
protobuf decoder when they visit your
page might be too much.

It may be trivial to cook up a mapping, but is there a single "obvious" mapping between the two that any two separate dev teams would naturally settle on? If two products supported PB data and could interoperate because they shared the same .proto spec, I wonder if they would still be able to interoperate if they independently introduced a JSON reflection of the same spec. There might be some arbitrary decisions to be made, e.g. should enum values be represented by a string (to be human-readable a la typical JSON) or by their integer value?

So is there an established mapping, and any open source implementations for generating JSON encoder/decoders from .proto specs?

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

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

发布评论

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

评论(6

墨离汐 2024-09-03 06:31:55

是的,从 Protocol Buffers 版本 3.0.0(2016 年 7 月 28 日发布)开始
“JSON 中定义明确的编码,作为二进制原型的替代方案
编码”
,如发行说明中所述

https://github.com /google/protobuf/releases/tag/v3.0.0

Yes, since Protocol Buffers version 3.0.0 (released July 28, 2016) there
is "A well-defined encoding in JSON as an alternative to binary proto
encoding"
as mentioned in the release notes

https://github.com/google/protobuf/releases/tag/v3.0.0

橘香 2024-09-03 06:31:55

据我所知, Protostuff 是用于 Java 上任何 PB 工作的项目,包括序列化它作为 JSON,基于协议定义。我自己没用过,只是听说不错。

From what I have seen, Protostuff is the project to use for any PB work on Java, including serializing it as JSON, based on protocol definition. I have not used it myself, just heard good things.

成熟的代价 2024-09-03 06:31:55

我需要从 GenerationMessageLite 编组到 JSON 对象,但不需要解组。我无法在 Pangea 的答案中使用 protobuf 库,因为它不适用于 LITE_RUNTIME 选项。我也不想为现有协议缓冲区生成更多编译代码,从而给我们已经很大的遗留系统带来负担。为了编组到 JSON,我使用了这个简单的解决方案来编组

    final Person gpb = Person.newBuilder().setName("Bill Monroe").build();
    final Gson gson = new Gson();
    final String jsonString = gson.toJson(gpb);

I needed to marshal from GeneratedMessageLite to a JSON object but did not need to unmarshal. I couldn't use the protobuf library in Pangea's answer because it doesn't work with the LITE_RUNTIME option. I also didn't want to burden our already large legacy system with generating more compiled code for the existing protocol buffers. For mashalling to JSON, I went with this simple solution to marshal

    final Person gpb = Person.newBuilder().setName("Bill Monroe").build();
    final Gson gson = new Gson();
    final String jsonString = gson.toJson(gpb);
风月客 2024-09-03 06:31:55

进一步的想法:如果 protobuf 对象具有 getter/setter,或适当命名的字段,则可以简单地使用 Jackson JSON 处理器数据绑定。默认情况下,它处理公共 getter、任何 setter 和公共字段,但这些只是默认的可见性级别,可以更改。如果是这样,Jackson 可以毫无问题地序列化/反序列化 protobuf 生成的 POJO。

我实际上已经将这种方法用于 Thrift 生成的对象;我唯一需要配置的就是禁用 Thrift 添加的各种“isXXX()”方法的序列化,以检查字段是否已被显式分配。

One further thought: if protobuf objects have getters/setters, or appropriately named fields, one could simply use Jackson JSON processor's data binding. By default it handles public getters, any setters and public fields, but these are just default visibility levels and can be changed. If so, Jackson can serialize/deserialize protobuf generated POJOs without problems.

I have actually used this approach with Thrift-generated objects; the only thing I had to configure there was to disable serialization of various "isXXX()" methods that Thrift adds for checking if a field has been explicitly assigned or not.

骄傲 2024-09-03 06:31:55

首先,我认为人们应该非常仔细地推理如何努力将数据集转换为 protobuff。这是我将数据集转换为 protobuffs

  1. 类型安全的原因:保证所考虑的数据的格式。
  2. 数据的未压缩内存占用。我提到未压缩的原因是因为压缩后 JSON 压缩和原始压缩的大小没有太大差异,但压缩有与之相关的成本。此外,序列化/反序列化速度几乎相似,事实上 Jackson json 比 protobuffs 更快。请查看以下链接以获取更多信息
    http://technicalrex.com/2014/06/23/performance-playground-jackson-vs-protocol- buffers/
  3. protobuffs需要通过网络传输很多。

也就是说,一旦您按照定义 ProtoBuff 定义的方式将数据集转换为 Jackson JSON 格式,那么就可以使用 Protostuff:JsonIoUtil:mergeFrom 函数轻松地将其直接映射到 ProtoBuff 格式。函数签名:

public static <T> void mergeFrom(JsonParser parser, T message, Schema<T> schema,  boolean numeric) 

参考 protostuff

First of all I think one should reason very carefully on putting an effort into converting a dataset to protobuffs. Here my reasons to convert a data-set to protobuffs

  1. Type Safety: guarantee on the format of the data being considered.
  2. uncompressed memory foot-print of the data. The reason I mention un-compressed is because post compression there isn't much of a difference in the size of JSON compressed and proto compressed but compression has a cost associated with it. Also, the serialization/de-serialization speed is almost similar, infact Jackson json is faster than protobuffs. Please checkout the following link for more information
    http://technicalrex.com/2014/06/23/performance-playground-jackson-vs-protocol-buffers/
  3. The protobuffs needs to be transferred over the network a lot.

Saying that once you convert your data-set to Jackson JSON format in the way that the ProtoBuff definition is defined then it can very easily be directly mapped to ProtoBuff format using the Protostuff:JsonIoUtil:mergeFrom function. Signature of the function :

public static <T> void mergeFrom(JsonParser parser, T message, Schema<T> schema,  boolean numeric) 

Reference to protostuff

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