protobuf.js translate proto file to JSON descriptor, repeated gets lost
Asked Answered
K

1

10

I'm using Protobuf.js to build a node package, containing our protocol and offering encode and decode functionality to Proto Messages defined in this package. I would be fine with using .proto files (The loading of .proto files happens at runtime) , but since the module needs to be usable on the client side and I can't pack the .proto files to my resolved .js-file (built with browserify), I need to use a way, that enables the packaging in the build.js.

Enter JSON Descriptors.

var jsonDescriptor = require("./awesome.json"); // exemplary for node

var root = protobuf.Root.fromJSON(jsonDescriptor);

The json file can be packed up (requirement resolved by browserify). Proto Type Defintions also are possible in .json

I translated my .proto file into a .json file and tried it with my example data. Unfortunately it failed with the repeated fields.

The .proto file looks kind of like this:

message Structure {
    map <int32, InnerArray> blocks = 1;
}

message Inner{
    int32 a = 1;
    int32 b = 2;
    bool c = 3;
}

message InnerArray{
    repeated Inner inners = 1;
}   

Which I translated into this JSON Descriptor

{
  "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
            }
        }
    }
  }
}

If I'm not mistaken there is the required attribute for a field.

When I encode and decode my example data it stops at the repeated field: (note that the map works fine).

{
  "blocks": {
    "0": {
      "inners": {}
    },
    ...

I also examined my root to find out how the loaded type looks and it looks exactly like my defintion EXCEPT that repeated is missing:

"InnerArray" : {
            "fields": {
                "inners" : {
                    "type" : "Inner",
                    "id" : 1
                }
            }
        },

How do I define a repeated field correctly in a JSON Descriptor?

If there is a way to pre include proto files and not load them at runtime, so that i can wrap them up with browserify, I would accept this as a solution, too.

Keck answered 2/6, 2017 at 12:18 Comment(0)
K
7

After browsing through the code, I found that you can not set required in a JSON Descriptor. The correct way is to set "rule": "repeated"; since a field is set with a Field Descriptor

Keck answered 2/6, 2017 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.