问题描述
我正在尝试使用msgpack(在C版本中,而不是在C ++中)替换我们自己的序列化方法,该方法基于主要xml.打包一些普通数据非常简单.但是,我们有很多基于k-v的结构,例如
I am trying to use msgpack (in c version, not c++) to replace our own serialization method, which is in primary xml based. It is quite straight forward to pack some ordinary data. However, we have a lot of k-v based structures like
struct Table {
struct Key {
// Multi-keys
int key1;
int key2;
};
struct Attr {
// Attributes
int attr1;
bool attr2;
char[8] attr3;
};
}
如何在msgpack-c中使用msg_pack_map打包多键表? (不幸的是,我们的系统被禁用了异常,所以我不能使用c ++版本的"msgpack.hpp")
How to pack multi-key table with msg_pack_map in msgpack-c? (unfortunately, our system is exception disabled, so I cannot use the c++ version "msgpack.hpp")
我的代码段:
msgpack_sbuffer sbuf;
msgpack_sbuffer_init(&sbuf);
msgpack_packer pk;
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
msgpack_packer_map(&pk, 10) // 10 pairs
for(int i= 0; i<10; ++i) {
// key
msgpack_pack_array(&pk, 2);
msgpack_pack_int(&pk, i);
msgpack_pack_int(&pk, 100+i);
// attr
msgpack_pack_array(&pk, 3);
msgpack_pack_int(&pk, 1);
msgpack_pack_true(&pk);
msgpack_pack_str(&pk, 7);
msgpack_pack_str_body(&pk, "example");
}
我假设在msgpack中,我们必须使用msgpack_pack_array来打包结构.
I assume in msgpack, we have to use msgpack_pack_array to pack struct.
我的代码正确吗,还是有更好的方法呢?
Is my code right, or is there any better way to do that?
推荐答案
是的,您是正确的.您的代码段中有两个细微的错误.msgpack_packer_map应该是msgpack_pack_map. msgpack_pack_str_body要求将字符串的长度作为第三个参数.
Yes, you are right. There are two subtle mistake in your code snippet.msgpack_packer_map should be msgpack_pack_map. msgpack_pack_str_body requires a length of the string as the third argument.
我更新了您的代码段,并添加了测试代码.
I updated your code snippet, and added test codes.
请参阅:
http://melpon.org/wandbox/permlink/RuZKLmzwStHej5TP
#include <msgpack.h>
#include <msgpack.hpp>
#include <map>
#include <iostream>
struct Key {
// Multi-keys
int key1;
int key2;
MSGPACK_DEFINE(key1, key2);
};
inline bool operator<(Key const& lhs, Key const& rhs) {
if (lhs.key1 < rhs.key1) return true;
if (lhs.key1 > rhs.key1) return false;
if (lhs.key2 < rhs.key2) return true;
return false;
}
struct Attr {
// Attributes
int attr1;
bool attr2;
std::string attr3;
MSGPACK_DEFINE(attr1, attr2, attr3);
};
int main() {
msgpack_sbuffer sbuf;
msgpack_sbuffer_init(&sbuf);
msgpack_packer pk;
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
msgpack_pack_map(&pk, 10); // 10 pairs
for(int i= 0; i<10; ++i) {
// key
msgpack_pack_array(&pk, 2);
msgpack_pack_int(&pk, i);
msgpack_pack_int(&pk, 100+i);
// attr
msgpack_pack_array(&pk, 3);
msgpack_pack_int(&pk, 1);
msgpack_pack_true(&pk);
msgpack_pack_str(&pk, 7);
msgpack_pack_str_body(&pk, "example", 7);
}
{
auto upd = msgpack::unpack(sbuf.data, sbuf.size);
std::cout << upd.get() << std::endl;
auto tbl = upd.get().as<std::map<Key, Attr>>();
}
msgpack_sbuffer_destroy(&sbuf);
}
那是c ++代码,但我在包装部分中使用了C API.您可以检查解压缩的msgpack对象.它还可以成功转换为C ++映射.
That is a c++ code but I use C API in the packing part. You can check the unpacked msgpack object. It is also converted as C++ map successfully.
以下是C API文档: https://github.com/msgpack/msgpack-c/wiki/v1_1_c_overview
Here is C API documentation:https://github.com/msgpack/msgpack-c/wiki/v1_1_c_overview
这篇关于如何使用msgpack-c打包多键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!