Skip to content

🌷Serialization framework based on boost.serialization、msgpack and json.

License

Notifications You must be signed in to change notification settings

chxuan/easypack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serialization framework based on boost.serialization、msgpack and json.

简介

easypack是基于boost.serializationmsgpackkapok的序列化框架,header-only,使用极其方便。

Getting started

首先下载easypack:

git clone https://github.com/chxuan/easypack.git

然后下载依赖的第三方库:

git submodule update --init --recursive

在编译时需要指定序列化框架,添加ENABLE_BOOST_SERIALIZATION宏定义来启用boost.serialization序列化框架,添加ENABLE_MSGPACK宏定义来启用msgpack序列化框架,添加ENABLE_JSON宏定义来启用json序列化框架。

Tutorial

  • base type

    int age = 20;
    std::string name = "Jack";
    easypack::pack p;
    p.pack_args(age, name);
    
    int age2;
    std::string name2;
    easypack::unpack up(p.get_string());
    up.unpack_args(age2, name2); 
    /* up.unpack_top(age2); */
    /* up.unpack_top(name2); */

可以看到,pack_args/unpack_args支持变参,并且还支持出栈式反序列化,使用非常方便。

  • std::tuple

    std::tuple<int, std::string> tp = std::make_tuple(10, "Tom");
    easypack::pack p;
    p.pack_args(tp);
    
    std::tuple<int, std::string> tp2;
    easypack::unpack up(p.get_string());
    up.unpack_args(tp2);

boost序列化默认不支持std::tuple类型,easypack序列化std::tuple提供了和序列化基本类型一样的接口。

  • STL type

    std::vector<int> vec { 1, 2 };
    std::unordered_map<int, std::string> m;
    m.emplace(1, "Hello");
    m.emplace(2, "world");
    easypack::pack p;
    p.pack_args(vec, m);
    
    std::vector<int> vec2;
    std::unordered_map<int, std::string> m2;
    easypack::unpack up(p.get_string());
    up.unpack_args(vec2, m2);
  • user-defined classes

    struct person_info
    {
        std::string name;
        int age;
        
    #ifdef ENABLE_BOOST_SERIALIZATION
        template<class Archive>
        void serialize(Archive& ar, const unsigned int)
        {
            ar & name;
            ar & age;
        }
    #endif
    
    #ifdef ENABLE_MSGPACK
        MSGPACK_DEFINE(name, age);
    #endif
    
    #ifdef ENABLE_JSON
        META(name, age);
    #endif
    };
    
    person_info info { "Jack", 20 };
    easypack::pack p;
    p.pack_args(info);
    
    person_info person;
    easypack::unpack up(p.get_string());
    up.unpack_args(person);

    boostmsgpackkapok序列化用户自定义类,更多细节请查看各自官网。

Warning

  • 不支持指针,仅支持栈对象序列化。

依赖性

  • boost.serialization
  • msgpack
  • kapok
  • c++11

兼容性

  • Linux x86_64 gcc4.9, gcc 5, gcc 6.
  • Windows x86_64 Visual Studio 2015

License

This software is licensed under the MIT license. © 2016 chxuan

About

🌷Serialization framework based on boost.serialization、msgpack and json.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages