Divyanshu Shekhar
Divyanshu Shekhar

Reputation: 252

Write protobuf for map< string, repeated map <string, string > >

{
    "response": {
        "status": [
            {
                "code": "red",
                "text": "random sentence"
            },
            {"code": "blue"}
        ],
        "recent": []
    }
}

For above json, I want to have proper protobuf syntax. Below is just an outline to show the idea. I know that below proto won't work.

  syntax = 'proto3';

  map< string, repeated map <string, string > > response = 1;

How can I write this in protobuf syntax?

Upvotes: 2

Views: 3234

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062540

You cannot; the thing on the right in a map cannot be repeated; you'd instead have a map<string, Foo> for some Foo that *hasarepeated` something.

Honestly, I think the key problem here is this statement:

For above json, I want to have proper protobuf syntax.

Protobuf is not a general purpose JSON tool; the JSON it chooses is strongly opinionated. If you want to map pre-existing JSON, then protobuf is not the right tool for you; instead, use any general purpose JSON tool - they'll usually be fine with it. Instead, think of protobuf as a serialization tool that happens to have a JSON variant output (in addition to the primary binary output), but which offers very little flexibiltiy re JSON layout.

Upvotes: 1

Related Questions