繁体   English   中英

如何将python字典放入protobuf消息中?

[英]how to put a python dictionary in a protobuf message?

假设我们有这个 Json blob:

{
  "thing": {
    "x": 1,
    "str": "hello,
    "params": {
      "opaque": "yes",
      "unknown": 1,
      "more": ...
    }
  }
}

params 的内容未知。 我们只知道它是一本字典。 我们如何定义可以解析它的 protobuf 消息?

// file: thing.proto
message Thing {
    uint32 x = 1;
    string str = 2;
    WhatGoesHere? params = 3;
}

[编辑] 移动解决方案以根据评论回答。

解决方案:使用谷歌提供的消息。

// file: solution.proto
import "google/protobuf/struct.proto";

message Solution1 {
    uint32 x = 1;
    string str = 2;
    google.protobuf.Struct params = 3;
}

message Solution2 {
    uint32 x = 1;
    string str = 2;
    map<string, google.protobuf.Value> params = 3;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM