How to describe in the proto file a json format with a double array?

3,046 views
Skip to first unread message

Michael Leonard

unread,
Sep 26, 2016, 1:56:12 PM9/26/16
to Protocol Buffers
Hi

I'm developing a golang server that will receive json in the following format. I can't work out how to write the description of this in a proto file and am hoping someone could help me! Thanks a lot in advance. 

{
    "key1": "asdfwefa",
    "key2": 13431,
    "key3": [
      [
        "asdfasdf",
        "cdcasdec",
        "dareceae"
      ],
      [
        "ggeqsase",
        "asdfdfgg",
        "asdreavf"
      ]
    ]
 }

The problem is the double array and the fact that the strings inside the array don't have named keys. I want to use something like following, but I'm not sure if there's a way to do this:

message MyObject {
    string key1 = 1;
    int32 key2 = 2;
    repeated Key3 key3 = 3;
}

message Key3 {
    repeated ...???...
}


Please note:
- I'm using proto3 syntax in my application in case that's important information.
- I can't modify the json format before it hits my golang server. However I could modify it when it arrives (somehow? perhaps marshal to a golang struct first, or modify the text string somehow - any easy solutions would be appreciated?) and then marshal it using the golang jsonpb package.


Thanks for any help

Mike


Kostiantyn Shchepanovskyi

unread,
Sep 27, 2016, 3:26:55 PM9/27/16
to Protocol Buffers
There is no way to workaround this. "key3" looks like a two-dimensional string array - it is not supported by protobuf. 

Feng Xiao

unread,
Sep 27, 2016, 3:46:56 PM9/27/16
to Kostiantyn Shchepanovskyi, Protocol Buffers
There is actually a new proto type designed to parse/represent arbitrary JSON data:

So you can define your proto as:

message MyObject {
    string key1 = 1;
    int32 key2 = 2;
    google.protobuf.ListValue key3 = 3;
}

and then you should be able to parse two-dimensional string arrays. There are two problems though:
1. It not only allows two-dimensional string array, but also allows any arbitrary JSON array data. For example, it will happily accept "[1, false, null, {\"a\": 2}]". That is, you lose the ability to do strict type checking.
2. go jsonpb does not support this new type yet, and if you want to use it, you probably need to add the support yourself (it's not on go-team's plan for Q4 as far as I know).

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Xingbao Zhou

unread,
Sep 13, 2018, 2:21:08 AM9/13/18
to Protocol Buffers
go jsonpb has supported google.protobuf.ListValue now. I just have a  try with the latest version.

在 2016年9月28日星期三 UTC+8上午3:46:56,Feng Xiao写道:
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages