C++ msgpack学习

2020-08-22  本文已影响0人  _给我一支烟_
#include <msgpack.hpp>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

struct Card
{
    std::string color;
    int number;
    Card()
    {
        color = "default";
        number = 0;
    }
    MSGPACK_DEFINE(color, number);
};

struct ThinkCard
{
    int type;
    Card card;
    ThinkCard()
    {
        type = 0;
    }
    MSGPACK_DEFINE(type, card);
};

struct AA
{
    int a = 1;
    int b = 2;
    MSGPACK_DEFINE_ARRAY(a, b);
};

void testMsgpack1()
{
    // -------------------- vector --------------------
    std::vector<std::string> vec;
    vec.push_back("Hello");
    vec.push_back("MessagePack");

    // pack
    std::stringstream ss;
    msgpack::pack(ss, vec);

    // unpack
    msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
    msgpack::object obj = oh.get();
    std::cout << obj << std::endl;  //=> ["Hello", "MessagePack"]

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

    // print
    std::vector<std::string>::iterator it = rvec.begin();
    for (; it != rvec.end(); ++it )
    {
        std::cout << *it << std::endl;
    }

    // -------------------- int --------------------
    int i{5};

    // pack
    std::stringstream ss2;
    msgpack::pack(ss2, i);

    // unpack
    msgpack::object_handle oh2 = msgpack::unpack(ss2.str().data(), ss2.str().size());
    msgpack::object obj2 = oh2.get();
    std::cout << obj2 <<std::endl;

    // convert
    int ii{};
    obj2.convert(ii);

    // print
    std::cout << ii << std::endl;

    // ------------------- double ------------------
    double d{3.1415926535699923};

    // pack
    std::stringstream ss21;
    msgpack::pack(ss21, d);

    // unpack
    msgpack::object_handle oh21 = msgpack::unpack(ss21.str().data(), ss21.str().size());
    msgpack::object obj21 = oh21.get();
    std::cout << obj21 <<std::endl;

    // convert
    double dd{};
    obj21.convert(dd);

    // print
    std::cout << dd << std::endl;

    // -------------------- string --------------------
    std::string str{"hello msgpack"};

    // pack
    std::stringstream ss3;
    msgpack::pack(ss3, str);

    // unpack
    msgpack::object_handle oh3 = msgpack::unpack(ss3.str().data(), ss3.str().size());
    msgpack::object obj3 = oh3.get();
    std::cout << obj3 <<std::endl;

    // convert
    std::string str2;
    obj3.convert(str2);

    // print
    std::cout << str2 << std::endl;

    // -------------------- struct --------------------
    Card card1;
    card1.color = "blue";
    card1.number = 13;

    // pack
    std::stringstream ss4;
    msgpack::pack(ss4, card1);

    // unpack
    msgpack::object_handle oh4 = msgpack::unpack(ss4.str().data(), ss4.str().size());
    msgpack::object obj4 = oh4.get();
    std::cout << obj4 << std::endl;

    // convert
    Card card2;
    obj4.convert(card2);

    // print
    std::cout << "color = " << card2.color << ", number = " << card2.number << std::endl;


    ThinkCard tc1;
    tc1.type = 99;
    tc1.card.color = "red";
    tc1.card.number = 11;

    // pack
    std::stringstream ss5;
    msgpack::pack(ss5, tc1);

    // unpack
    msgpack::object_handle oh5 = msgpack::unpack(ss5.str().data(), ss5.str().size());
    msgpack::object obj5 = oh5.get();
    std::cout << obj5 << std::endl;

    // convert
    ThinkCard tc2;
    obj5.convert(tc2);

    // print
    std::cout << "type = " << tc2.type
              << ", color = " << tc2.card.color << ", number = " << tc2.card.number << std::endl;
              
    //---------------------------------
    AA aa;
    aa.a = 1;
    aa.b = 2;
    // pack
    std::stringstream ss6;
    msgpack::pack(ss6, aa);
    // unpack
    msgpack::object_handle oh6 = msgpack::unpack(ss6.str().data(), ss6.str().size());
    msgpack::object obj6 = oh6.get();
    std::cout << obj6 << std::endl;
    
    std::string strSs6 = ss6.str();
    std::cout << strSs6 << std::endl;
    //转换成16进制
    char signHex[256] = {0};
    for(int i = 0 ; i < strSs6.length(); ++i)
    {
        sprintf(signHex + i*2, "%02x", strSs6[i]);
    }
    std::cout << std::string(signHex) << std::endl;


}

void testMsgpack2()
{
    msgpack::sbuffer  sbuf;
    msgpack::packer<msgpack::sbuffer>   pker(&sbuf);

// 序列化
    pker.pack_map(3);
    pker.pack(std::string("type"));
    pker.pack(3);
    pker.pack(std::string("ratio"));
    pker.pack(2.15);
    pker.pack(std::string("msg"));
    pker.pack(std::string("hello world"));

// 反序列化
    msgpack::unpacked  unpack;
    msgpack::unpack(unpack, sbuf.data(), sbuf.size());

// 直接输出结果
    msgpack::object  obj = unpack.get();
    std::cout << obj << std::endl;

// 访问具体键值对
//    msgpack::object_kv*  pkv;
//    msgpack::object_kv*  pkv_end;
//    msgpack::object      pk, pv;
//    if(obj.via.map.size > 0)
//    {
//        pkv = obj.via.map.ptr;
//        pkv_end = obj.via.map.ptr + obj.via.map.size;
//
//        do
//        {
//            pk = pkv->key;
//            pv = pkv->val;
//
//            std::cout << pk << ", " << pv << std::endl;
//
//            ++pkv;
//        }
//        while (pkv < pkv_end);
//    }
    if (obj.type == msgpack::type::MAP)
    {
        std::map<std::string, msgpack::object> kv;
        kv = obj.as<std::map<std::string, msgpack::object>>();
        for (auto it = kv.begin(); it != kv.end(); ++it)
        {
            std::cout << it->first << ", " << it->second << std::endl;
        }
    }
}

int main()
{
  testMsgpack1();  
  return 0;
}

// g++ test1.cpp -I/home/hp/tools/messagepack/msgpack-c/include
//

上一篇下一篇

猜你喜欢

热点阅读