【问题标题】:Write protobuf for map< string, repeated map <string, string > >为map<string,重复map<string,string>>写protobuf
【发布时间】:2021-06-22 06:22:12
【问题描述】:
{
    "response": {
        "status": [
            {
                "code": "red",
                "text": "random sentence"
            },
            {"code": "blue"}
        ],
        "recent": []
    }
}

对于上面的 json,我想要正确的 protobuf 语法。下面只是一个大纲来展示这个想法。我知道下面的 proto 是行不通的。

  syntax = 'proto3';

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

如何用 protobuf 语法写这个?

【问题讨论】:

    标签: protocol-buffers protobuf-net


    【解决方案1】:

    你不能; map 右边的东西不能是 repeated;取而代之的是 map&lt;string, Foo&gt; 用于一些 Foo *hasarepeated` 的东西。

    老实说,我认为这里的关键问题是这句话:

    对于上面的 json,我想要正确的 protobuf 语法。

    Protobuf 不是通用的 JSON 工具;它选择的 JSON 强烈主张。如果你想映射预先存在的 JSON,那么 protobuf 不是适合你的工具;相反,请使用任何通用 JSON 工具 - 他们通常会使用它。相反,将 protobuf 视为一个序列化工具,恰好有一个 JSON 变体输出(除了主要的二进制输出),但它提供的 JSON 布局的灵活性很小。

    【讨论】:

    • message Extra { map infos = 1; } message ExtraList { 重复的 Extra extra_list = 1; } map 响应 = 16;我能够以这种方式制作我的原型,但我只想要一个字典列表而不命名每个变量。就像在 json 表示中一样,需要添加“extra_list”和“infos”。你知道其他方法可以做到这一点吗?
    • @DivyanshuShekhar 不,基本上
    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2013-12-18
    • 1970-01-01
    • 2013-05-24
    • 2014-01-29
    相关资源
    最近更新 更多