加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

msgpack序列化反序列化

(2020-07-16 09:20:21)
标签:

msgpack

序列化

反序列化

分类: c/cpp
1.简介
MessagePack(以下简称MsgPack)一个基于二进制高效的对象序列化类库,可用于跨语言通信。
它可以像JSON那样,在许多种语言之间交换结构对象;
但是它比JSON更快速也更轻巧。
支持Python、Ruby、Java、C/C++等众多语言。
比Google Protocol Buffers还要快4倍

2.下载
msgpack官方主页:http://msgpack.org/
github主页:https://github.com/msgpack/msgpack

3.demo
#include <iostream>
#include <fstream>
#include "msgpack/include/msgpack.hpp"
#include <list>
#include <map>

using std::list;
using std::map;
using std::string;

bool to_file(const string &filename)
{
    std::ofstream ofs(filename.c_str(), std::ios_base::binary);
    if (!ofs.good() || !ofs.is_open())
    {
        return false;
    }

    msgpack::sbuffer buffer;
    msgpack::packer<msgpack::sbuffer> pack(buffer);
    pack.pack("123456");

    std::list<string> lt_data;
    lt_data.push_back("111");
    lt_data.push_back("222");
    pack.pack(lt_data);

    std::map<string, string> mp_data;
    mp_data.insert(std::pair<string, string>("a", "aaaaa"));
    mp_data.insert(std::pair<string, string>("b", "bbbbb"));
    pack.pack(mp_data);

    ofs.write(buffer.data(), buffer.size());
    ofs.close();

    return true;
}

bool from_file(const string &filename)
{
    std::ifstream ifs(filename.c_str(), std::ifstream::binary);
    if (!ifs.good() || !ifs.is_open())
    {
        return false;
    }
    ifs.seekg(0, ifs.end);
    int length = ifs.tellg();
    ifs.seekg(0, ifs.beg);

    msgpack::unpacker unpack;
    unpack.reserve_buffer(length);
    ifs.read(unpack.buffer(), length);
    unpack.buffer_consumed(length);

    msgpack::object_handle result;

    string str;
    unpack.next(result);
    result.get().convert(str);

    list<string> lt_data;
    unpack.next(result);
    result.get().convert(lt_data);
    

    map<string,string> mp_data;
    unpack.next(result);
    result.get().convert(mp_data);

    return true;
}

int main(int argn, char **args)
{
    string filename = "temp.file";
    to_file(filename);
    from_file(filename);

    return 0;
}


#include <msgpack.hpp>
#include <vector>
#include <string>
#include <iostream>

int main()
{
    std::vector<std::string> _vecString;
    _vecString.push_back("Hello");
    _vecString.push_back("world");

    // pack
    msgpack::sbuffer _sbuffer;
    msgpack::pack(_sbuffer, _vecString);
    std::cout << _sbuffer.data() << std::endl;

    // unpack
    msgpack::unpacked msg;
    msgpack::unpack(&msg, _sbuffer.data(), _sbuffer.size());
    msgpack::object obj = msg.get();
    std::cout << obj << std::endl;

    // convert
    std::vector<std::string> _vecRString;
    obj.convert(&_vecRString);

    // print
    for(size_t i = 0; i < _vecRString.size(); ++i)
    {
        std::cout << _vecRString[i] << std::endl;
    }

    return 0;
}

#include <msgpack.hpp>
#include <vector>
#include <string>
#include <iostream>

int main()
{
    // msgpack stream

    // use msgpack::packer to pack multiple objects.
    msgpack::sbuffer buffer_;
    msgpack::packer pack_(&buffer_);
    pack_.pack(std::string("this is 1st string"));
    pack_.pack(std::string("this is 2nd string"));
    pack_.pack(std::string("this is 3th string"));

    // use msgpack::unpacker to unpack multiple objects.
    msgpack::unpacker unpack_;
    unpack_.reserve_buffer(buffer_.size());
    memcpy(unpack_.buffer(), buffer_.data(), buffer_.size());
    unpack_.buffer_consumed(buffer_.size());

    msgpack::unpacked result_;
    while (unpack_.next(&result_))
    {
        std::cout << result_.get() << std::endl;
    }

    return 0;
}

#include <msgpack.hpp>
#include <string>
#include <iostream>
#include <sstream>

int main(void)
{
    msgpack::type::tuple<int, bool, std::string> src(1, true, "example");

    // serialize the object into the buffer.
    // any classes that implements write(const char*,size_t) can be a buffer.
    std::stringstream buffer;
    msgpack::pack(buffer, src);

    // send the buffer ...
    buffer.seekg(0);

    // deserialize the buffer into msgpack::object instance.
    std::string str(buffer.str());

    msgpack::object_handle oh =
        msgpack::unpack(str.data(), str.size());

    // deserialized object is valid during the msgpack::object_handle instance is alive.
    msgpack::object deserialized = oh.get();

    // msgpack::object supports ostream.
    std::cout << deserialized << std::endl;

    // convert msgpack::object instance into the original type.
    // if the type is mismatched, it throws msgpack::type_error exception.
    msgpack::type::tuple<int, bool, std::string> dst;
    deserialized.convert(dst);

    // or create the new instance
    msgpack::type::tuple<int, bool, std::string> dst2 =
        deserialized.as<msgpack::type::tuple<int, bool, std::string> >();

    return 0;
}

3.更多
/questions/44725299/messagepack-c-how-to-iterate-through-an-unknown-data-structure

0

阅读 收藏 喜欢 打印举报/Report
前一篇:a股上涨征兆
后一篇:网线拔出
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有