Artur Baruchi
Artur Baruchi

Reputation: 11

Unamed Array - protobuf

I want to receive a list (array) of message using protobuf. The idea would be to receive something like:

[
  {
    "field1": "value1",
    "field2": "value2"
  },
  {
    "field1": "value1",
    "field2": "value2"
  }
]

However, when defining protobuf messages I can get ride of the top name in this array. For example:

message MyFields {
  string field1 = 1;
  string field2 = 2;
}

message MyMessage {
  repeated MyFields some_identifier = 1;
}

Which would create something like:

{
  "some_identifier":
 [
   {
    "field1": "value1",
    "field2": "value2"
   },
   {
    "field1": "value1",
    "field2": "value2"
   }
 ]
}

Not even sure if this is possible with protobuf.

Thanks!

Upvotes: 1

Views: 116

Answers (2)

Matteo
Matteo

Reputation: 39370

did you try with a repeated Map? Like:

message MyFields {
  map<string, string> MyMap = 1;
}

message MyMessage {
  repeated MyFields some_identifier = 1;
}

Upvotes: 0

DazWilkin
DazWilkin

Reputation: 40061

It is not possible.

Protobuf requires that messages comprise fields and fields have a name, a type (and a field number).

See Definining a Message Type

In your example, the repeated message field would have no name and this isn't permitted.

Upvotes: 1

Related Questions