你如何编码Map< String,Object>使用协议缓冲区? [英] How would you encode a Map<String, Object> using Protocol Buffers?

查看:167
本文介绍了你如何编码Map< String,Object>使用协议缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Protocol Buffers

我的消息格式应包含Map< String,Object> entries ...但是如何编写.proto定义?

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

据我所知,Protocol Buffers没有内置Map类型。我可以使用重复字段来模拟它。但我遇到的最大问题是,您需要定义所有类型。我希望我的信息灵活,所以我不能指定类型。

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.

任何想法?

推荐答案

我要用一个元组建模一个键和一个值(可能是每个类型的一个值字段)。然后只需要重复列出该元组类型。你需要在代码中构建地图。

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.

听起来你所拥有的非结构水平可能不适合PB。

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

这篇关于你如何编码Map&lt; String,Object&gt;使用协议缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆