在 proto3 中将对象作为数组传递给 body

Passing object as array in body in proto3

我想知道如何在不指定键的情况下将数组作为消息正文传递。我可以轻松做到:

message TypeResponse {
  message Type {
    string ID = 1;
    string Name = 2;
    string Description = 3;
    string IsMobile = 4;
    string IsTablet = 5;
    string IsDesktop = 6;
  }
  repeated Type types = 1;
}

那会回应:

{
  "types": [
    {
      "ID": 1
      ...
    }
  ]
}

我想按照以下方式构建我的响应以匹配我的 REST API:

[
    {
      "ID": 1
      ...
    },
    {
      "ID": 2
      ...
    }
]

Proto 要求顶级概念是一条消息,它溢出到 JSON 映射中。

您可以做的是跳过第一个字符直到到达 [ 个字符,然后删除最后一个字符 ]。 JSON 的输出格式由 Proto3 spec 指定,因此您可以合理地依赖格式。