pranay jain
pranay jain

Reputation: 372

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;
}

Upvotes: 13

Views: 21404

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062510

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;

Upvotes: 20

Related Questions