我使用Protobuf.js建立一个节点包,其中包含我们的协议,并提供编码和解码功能,在这个包中定义的原消息。我会很好的使用.proto文件(加载的.proto文件在运行时发生),但由于该模块需要在客户端使用,我不能将.proto文件打包到我解析的.js文件中(使用browserify构建),我需要使用一种方法,它可以在build.js中打包。protobuf.js原型文件转换成JSON描述,反复丢失
输入JSON描述符。
var jsonDescriptor = require("./awesome.json"); // exemplary for node
var root = protobuf.Root.fromJSON(jsonDescriptor);
json文件可以打包(需要通过browserify解决)。原型Defintions在以.json
也有可能我翻译.proto文件转换成一个以.json文件,并与我的示例数据试了一下。 不幸的是,它重复的领域失败了。
的.proto文件看起来有点像这样:
message Structure {
map <int32, InnerArray> blocks = 1;
}
message Inner{
int32 a = 1;
int32 b = 2;
bool c = 3;
}
message InnerArray{
repeated Inner inners = 1;
}
其中我翻译成这个JSON描述
{
"nested": {
"Structure": {
"fields": {
"blocks": {
"type": "InnerArray",
"id": 1,
"map" : true,
"keyType" : "int32"
}
}
},
"InnerArray" : {
"fields": {
"inners" : {
"repeated" : true,
"type" : "Inner",
"id" : 1
}
}
},
"Inner" : {
"fields" : {
"a" : {
"type" : "int32",
"id" : 1
},
"b" : {
"type" : "int32",
"id" : 2
},
"c" : {
"type" : "bool",
"id" : 3
}
}
}
}
}
如果我没有记错的有一个字段的required属性。
当我编码和解码停靠在现场重复我的示例数据:(请注意,地图正常工作)。
{
"blocks": {
"0": {
"inners": {}
},
...
我也检查了我的根,找出负荷型的外观和它看起来完全像我确定指标EXCEPT反复丢失:
"InnerArray" : {
"fields": {
"inners" : {
"type" : "Inner",
"id" : 1
}
}
},
如何定义重复场在JSON描述符中正确吗?
如果有一种方法可以预先包含proto文件,而不是在运行时加载它们,这样我可以用browserify将它们包装起来,我也会接受这种解决方案。