Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent a JSON object variable with Proto definition

I want to define a request message in gRPC which should have a Json Object as a field For e.g.

message UserRequest{
    string name = 1;
    string city = 2;
    string email = 3;
    metainfo = 4;//A Json Object variable which can have any number of elements
}

How do I represent the metainfo property within proto definition? I have tried using below definition but it didn't work.

message UserRequest{
    string name = 1;
    string city = 2;
    string email = 3;
    google.protobuf.Any metainfo = 4;
}
like image 268
pranay jain Avatar asked Jan 02 '20 10:01

pranay jain


1 Answers

I think you want a .google.protobuf.Struct, via struct.proto - this essentially encapsulates a map<string, Value> fields, and is broadly akin to what you would want to describe via JSON. Additionally, Struct has custom JSON handling, as mentioned in the file:

The JSON representation for Struct is JSON object.

So:

    .google.protobuf.Struct metainfo = 4;
like image 136
Marc Gravell Avatar answered Oct 18 '22 18:10

Marc Gravell