协议缓冲区中的字典

发布于 2024-10-02 22:27:15 字数 46 浏览 0 评论 0原文

有没有办法使用协议缓冲区序列化字典,或者如果需要的话我必须使用 Thrift?

Is there any way to serialize a dictionary using protocol buffers, or I'll have to use Thrift if I need that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪心 2024-10-09 22:27:15

对于未来的答案寻求者,ProtoBuf 现在原生支持地图

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

For future answer seekers, ProtoBuf now supports Maps natively:

message MapMessage
{
    map<string, string> MyMap = 1;
}
各空 2024-10-09 22:27:15

Protobuf 规范现在原生支持字典(地图)

原始答案

人们通常将字典写成键值对列表,然后在另一端重建字典。

message Pair {
   string key = 1;
   string value = 2;
}

message Dictionary {
   repeated Pair pairs = 1;
}

Protobuf specification now supports dictionaries (maps) natively.

Original answer

People typically write down the dictionary as a list of key-value pairs, and then rebuild the dictionary on the other end.

message Pair {
   string key = 1;
   string value = 2;
}

message Dictionary {
   repeated Pair pairs = 1;
}
情未る 2024-10-09 22:27:15

您可以检查 ProtoText 包。

假设您要将字典 person_dict 序列化为 personbuf_pb2 模块中定义的预定义 PersonBuf protobuf 对象。

在这种情况下,要使用 ProtoText,

import ProtoText
from personbuf_pb2 import PersonBuf

obj = PersonBuf()
obj.update(person_dict)

You can check the ProtoText package.

Assume you want to serialize a dict person_dict to a pre-defined PersonBuf protobuf object defined in personbuf_pb2 module.

In this case, to use ProtoText,

import ProtoText
from personbuf_pb2 import PersonBuf

obj = PersonBuf()
obj.update(person_dict)
oО清风挽发oО 2024-10-09 22:27:15

我首先评论@Flassari的答案,因为它真的很方便。

但是,就我而言,我需要 map 其中:

enum Type {
    Undefined = 0;
    Square = 1;
    Circle = 2;
}

message AnyModel {
    string Name = 1;
}

这里我只想返回一个字典,对于每种类型,该字典包含 AnyModel 列表

但是,我没有找到比 @JesperE 提出的更好的解决方法,因此我执行了以下操作: (as you can不使用枚举作为映射中的键

message MyRPCBodyCall {
    map<string, AnyModels> Models = 1;
}

enum Type {
    Undefined = 0;
    Square = 1;
    Circle = 2;
}

message AnyModel {
    string Name = 1;
}

message AnyModelArray {
    repeated AnyModel AnyModels = 1;
}

这里我使用我从服务器/客户端选择的代码语言将枚举转换为字符串

所以这两种方法实际上都是有效的答案IMO,取决于您的要求。

I firstly comment the @Flassari 's answer as it is really convenient.

However, in my case, I needed map<Type, repeated AnyModel> where :

enum Type {
    Undefined = 0;
    Square = 1;
    Circle = 2;
}

message AnyModel {
    string Name = 1;
}

Here I just want to return a dictionary that, for each type, contain a list of AnyModel

However, I didn't find a better workaround than the one proposed by @JesperE so I did the following: (as you can't use enum as key in map)

message MyRPCBodyCall {
    map<string, AnyModels> Models = 1;
}

enum Type {
    Undefined = 0;
    Square = 1;
    Circle = 2;
}

message AnyModel {
    string Name = 1;
}

message AnyModelArray {
    repeated AnyModel AnyModels = 1;
}

Here I convert from/to string my enum using my chosen code languages from both server/client side

So both approaches are actually valid answers IMO, depends on your requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文