如何对 Map进行编码? 使用协议缓冲区?

发布于 2024-07-27 02:39:49 字数 486 浏览 2 评论 0原文

我正在尝试使用 用于消息序列化的协议缓冲区

我的消息格式应包含 Map< 字符串、对象> 条目...但是我该如何编写 .proto 定义?

据我所知,Protocol Buffers 没有内置的 Map 类型。 我可以使用重复字段围绕它进行建模。 但我遇到的一个大问题是,您需要定义所有类型。 我希望我的消息灵活,所以我无法指定类型。

有任何想法吗?

I'm trying to use Protocol Buffers for message serialization.

My message format should contain Map< String, Object > entries ... but how do I write the .proto definition?

As far as I know, Protocol Buffers does not have a build-in Map type. I could model around that using repeating fields. But the big problem I have is, that you need to define all your types. I want my message to be flexible, so I can't specify the types.

Any ideas?

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

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

发布评论

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

评论(1

可可 2024-08-03 02:39:49

我会用一个键和一个值来建模一个元组(可能值可能是每种类型一个值字段)。 然后就有该元组类型的重复列表。 您需要用代码构建地图本身。

当你说你不能指定类型时 - 你在谈论哪种类型? 如果元组中的每种类型都有一个可选字段,那么这将适用于所有原语 - 并且您可以通过将消息序列化为字节字符串来映射消息。

听起来你的“非结构化”水平可能不太适合 PB。

I'd model a tuple with a key and a value (probably one value field per type that the value could be). Then just have a repeated list of that tuple type. You'd need to build the map itself in code.

When you say you can't specify the types - what sort of types are you talking about? If you have an optional field of each type in the tuple, then that would cope for all the primitives - and you could map messages by serializing them into a byte string.

It sounds like the level of "unstructure" you have may not be a good fit for PB though.

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