0

Tring to *.proto -> json -generator-> other things

Tried pbjs, but the json can only hold one option, and no comment.

Is there any way to use the protoc to generate the json that represent the protos ?

Write a protoc generator is harder than work with the json.

EDIT

For example

With a proto like this

syntax = "proto2";

option java_package = "com.google.apps.jspb.proto";

import "google/protobuf/descriptor.proto";

package jspb.test;

message Empty {
}

enum OuterEnum {
  FOO = 1;
  BAR = 2;
}

message EnumContainer {
  optional OuterEnum outer_enum = 1;
}

message Simple1 {
  required string a_string = 1;
  repeated string a_repeated_string = 2;
  optional bool a_boolean = 3;
}
// etc etc

pbjs can generate a json that represent the proto def like this

{
  "options": {
    "java_package": "com.google.apps.jspb.proto",
    "java_multiple_files": true
  },
  "nested": {
    "jspb": {
      "nested": {
        "test": {
          "nested": {
            "Empty": {
              "fields": {}
            },
            "OuterEnum": {
              "values": {
                "FOO": 1,
                "BAR": 2
              }
            },
            "EnumContainer": {
              "fields": {
                "outerEnum": {
                  "type": "OuterEnum",
                  "id": 1
                }
              }
            },
            "Simple1": {
              "fields": {
                "aString": {
                  "rule": "required",
                  "type": "string",
                  "id": 1
                },
                "aRepeatedString": {
                  "rule": "repeated",
                  "type": "string",
                  "id": 2
                },
                "aBoolean": {
                  "type": "bool",
                  "id": 3
                }
              }
            }
          }
        }
      }
    }
  }
}

So I can work with the json instead of protoc & proto. But pbjs is unmaintained, has a lot problem, trying to get these from protoc.

UPDATE

Seems what I want is the descriptor, just need a way to get the descriptor data out of protoc.

wener
  • 7,191
  • 6
  • 54
  • 78
  • Can you please clarify your question? (perhaps show a simple `.proto` file and the JSON output you are expecting). Are you trying to use `protoc` to generate code that will convert protobuf encoded data into JSON? If so `protoc` will generate code that helps with this, but you would need to write a bit of code to load the data and output as JSON (e.g. in go that might mean using [protojson](https://pkg.go.dev/google.golang.org/protobuf@v1.28.1/encoding/protojson)). – Brits Aug 12 '22 at 03:43

1 Answers1

0

protoc support -o to output descriptor set in binary format, almost include everything.


Problem solved by making protoc-gen-debug write the request as a json file, so I get a parsed proto descriptor as json.

wener
  • 7,191
  • 6
  • 54
  • 78