在C ++中使用MessagePack反序列化异构映射 [英] Deserializing a heterogeneous map with MessagePack in C++

查看:139
本文介绍了在C ++中使用MessagePack反序列化异构映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MessagePack与C ++一起使用,并且试图反序列化此Python映射的等效项:

I’m using MessagePack with C++ and I’m trying to deserialize the equivalent of this Python map:

{'metadata': {'date': '2014-06-25', 'user_id': 501},
 'values': [3.0, 4.0, 5.0],
 'version': 1}

顶级对象是带有字符串键的映射,但是值的类型完全不同.我的代码提前知道对象的结构.我应该能够声明一个整数,然后告诉我的反序列化代码,"version键的值是一个整数,因此请将该整数的值放入此内存地址中."

The top-level object is a map with string keys, but the values are of completely different types. My code knows ahead of time what the structure of the object should be; I ought to be able to declare an integer and then tell my deserialization code, "The value of the version key is an integer, so put the value of that integer into this memory address."

问题是我什至不确定如何使我的C ++代码可以将此结构视为地图.我希望做类似的事情

The problem is that I’m not even sure how to get to the point where my C++ code can treat this structure as a map. I would expect to do something like

msgpack::unpacker unpacker;
// ...copy the data into unpacker's buffer...

msgpack::unpacked message;
std::map<std::string, anything> output_map;

unpacker.next(&message);
msgpack::object obj = message.get();
obj.convert(&output_map);

int version_number = output_map.at("version");

在这里是否可以使用任何可能的类型(anything)? MessagePack文档仅包含一些琐碎的示例,并且

Is there any possible type (anything) that would work here? The MessagePack documentation has only trivial examples, and this blog post is better but doesn’t cover this use case.

推荐答案

我最终对此进行了批评,并撰写了

I ended up punting on this and writing my own text-based serialization format. It’s not generally useful like MessagePack is, but it allows me to paper over these static-typing woes.

这篇关于在C ++中使用MessagePack反序列化异构映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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