Protobuf.js translates the proto file into a JSON descriptor, repeated, lost

I am using Protobuf.js to build a node package that contains our protocol and offers encoding and decoding functions for the Proto protocols defined in that package. It would be nice for me to use .proto files (loading .proto files happens at runtime), but since the module has to be used on the client side and I cannot package the .proto files into my resolved .js file (built with a browser), I need to use a way that allows packaging in build.js.

Enter JSON descriptors.

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

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

      

The json file can be packaged (requirement allowed by browser). Proto Type Defintions are also possible in .json

I translated my .proto file into a .json file and tried it with my example data. Unfortunately this is not with duplicate fields.

The .proto file looks something 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;
}   

      

What 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 a required attribute for the field.

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

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

      

I also looked into my root to see what the loaded type looks like, and it looks exactly the same as my EXCEPT error , which is repeated:

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

      

How do I correctly identify a duplicate field in a JSON descriptor?

If there is a way to pre-enable the proto files and not load them at runtime so that I can process them with the browser, I would take that as a solution too.

+3


source to share


1 answer


After looking at the code, I found that you cannot set the required JSON descriptor. The correct way is to install "rule": "repeated"

; since the field is specified with the field descriptor



+2


source







All Articles