json - 将对象数组表示为 protobuf 消息
问题描述
我想创建一个表示对象数组的 protobuf 消息。
例子
[
{
"firstKey": "firstValue",
"secondKey": "secondValue",
},
{
"firstKey": "firstValue",
"secondKey": "secondValue",
},
...
]
伪代码(不是有效的语法)
syntax = "proto3";
message Entry {
string firstKey = 1;
string secondKey = 2;
}
repeated message Response {
...Entry;
}
我找不到办法做到这一点。甚至有可能还是我被迫像这样嵌套它?
syntax = "proto3";
message Entry {
string firstKey = 1;
string secondKey = 2;
}
message Response {
repeated Entry data = 2;
}
解决方案
repeated
is only permitted on message fields (use of type) not (definition of) types:
https://developers.google.com/protocol-buffers/docs/overview#specifying_field_rules
This makes sense as your alternative would require that the type is always repeated which is less useful; if you have "many of" you're likely to want to use "one of" too.
You can nest the definition so that it is only applicable to the referencing type:
https://developers.google.com/protocol-buffers/docs/overview#nested
推荐阅读
- javascript - 机器人仅提及
- java - 从微服务架构中的查询服务执行任务的 DAG
- r - R根据前面的结果逐组计算
- python - 重写一个文件夹中的文件并重命名它们
- angular - 在角度指令中定义 @HostListener 事件的事件顺序
- java - 为什么类名以数字开头?
- javascript - 如何将图像 URL 传递给 React 路由器?图像未显示在组件中,但图像已通过?
- mysql - 为什么 phpmyadmin 连接到 mysql 时出错?
- html - 如何使用包含“_ngcontent-c1”标签的 rvest html 内容进行网络抓取?
- android - 收到通知时获取 java.lang.IllegalAccessError