用户定义的类序列化,C++和消息包
我对 msgpack 还很陌生。我需要序列化一个对象(用户定义类的实例),其中包含指针(内部树、哈希等)和一些基本类型属性。
到目前为止,我可以执行 msgpack.org wiki 快速示例中所做的操作,只需将类序列化为 msgpack::sbuffer
,然后读取缓冲区以进行反序列化。
但现在,我想将该缓冲区发送到文件,或将序列化结果发送到文件,然后将其反序列化。
谁能给我一些关于如何做的提示?我浏览和阅读了足够多的内容,已经厌倦了:)
我的代码如下所示:
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, cluster); //cluster is the instance of my class clustering
//HERE I SHOULD SEND THE BUFFER TO A STREAM FILE, AND THEN LOAD IT IN THE UNPACK;
msgpack::unpacked msg;
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();
clustering clustUnser
obj.convert(&clustUnser);
谢谢大家!
最好的,
卢修斯。
I am quite new trying msgpack. I need to serialize an object (instance of an user defined class), that contains pointers (internal tree, hashes,etc), and some basic types attributes.
Until now I can do what is done in the quick example of msgpack.org wiki, just serialize the class to a msgpack::sbuffer
, and then read the buffer to unserialize.
But now, I want to send that buffer to a file, or the serialization result to a file and then unserialize it.
Can anyone give me some tip on how to do it? I browse and read enough to get tired of it :)
My code look like this:
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, cluster); //cluster is the instance of my class clustering
//HERE I SHOULD SEND THE BUFFER TO A STREAM FILE, AND THEN LOAD IT IN THE UNPACK;
msgpack::unpacked msg;
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();
clustering clustUnser
obj.convert(&clustUnser);
thanks everybody!
bests,
Luchux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从这里的示例:
http://wiki.msgpack.org/pages/viewpage。 action?pageId=1081387
看起来 sbuf.data() 将返回地址,而 sbuf.size() 将返回要写入二进制文件的数据的大小 文件。
当您想要从二进制文件加载数据时,请将其读入您分配的缓冲区,然后将地址和大小传递给 msgpack::unpack 调用。
From the example here:
http://wiki.msgpack.org/pages/viewpage.action?pageId=1081387
it looks like sbuf.data() would return the address, and sbuf.size() would return the size, of the data which you would write to the binary file.
When you want to load the data from a binary file, read it into a buffer you've allocated and then pass the address and size to the msgpack::unpack call.