zoukankan      html  css  js  c++  java
  • USCiLab cereal json 序列化

    cereal json 序列化

    https://blog.csdn.net/sunnyloves/article/details/51373793?utm_source=blogxgwz8

     http://uscilab.github.io/cereal/

    https://github.com/USCiLab/cereal

    #include <iostream>
    #include <fstream>
    #include <string>
    #include "cereal/archives/binary.hpp"
    #include "cereal/archives/xml.hpp"
    #include "cereal/archives/json.hpp"
    #include "cereal/types/unordered_map.hpp"
    #include "cereal/types/memory.hpp"
    #include "cereal/types/string.hpp"  //一定要包含此文件,否则无法将std::string序列化为二进制形式, 
    #include <Winsock2.h>
    
    #include <cereal/types/vector.hpp>
    using namespace std;
    
    
    
    struct MyRecord
    {
        int x, y;
        float z;
    
        template <class Archive>
        void serialize(Archive & ar)
        {
            ar(x, y, z);
        }
    
        friend std::ostream& operator<<(std::ostream& os, const MyRecord& mr);
    };
    
    std::ostream& operator<<(std::ostream& os, const MyRecord& mr)
    {
        os << "MyRecord(" << mr.x << ", " << mr.y << "," << mr.z << ")
    ";
        return os;
    }
    
    struct SomeData
    {
        int32_t id;
        std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;
    
        SomeData(int32_t id_ = 0) : id(id_), data(new std::unordered_map<uint32_t, MyRecord>)
        {
    
        }
    
        template <class Archive>
        void save(Archive & ar) const
        {
            ar(id, data);
        }
    
        template <class Archive>
        void load(Archive & ar)
        {
            ar(id, data);
        }
    
        void push(uint32_t, const MyRecord& mr)
        {
            data->insert(std::make_pair(100, mr));
        }
    
        void print()
        {
            std::cout << "ID : " << id << "
    ";
            if (data->empty())
                return;
            for (auto& item : *data)
            {
                std::cout << item.first << "	" << item.second << "
    ";
            }
        }
    };
    
    void Serialization_XML()
    {
        {
            std::ofstream os("my.xml");
    
            cereal::XMLOutputArchive archive(os);
    
            int age = 26;
            std::string name = "lizheng";
    
            //#define CEREAL_NVP(T) ::cereal::make_nvp(#T, T)
            archive(CEREAL_NVP(age), cereal::make_nvp("Name", name));
    
            //os.close();  //注意:这里不能显示关闭ofstream,否则序列化无法写入到文件
        }
    
        {
            std::ifstream is("my.xml");
            cereal::XMLInputArchive archive(is);
    
            int age;
            std::string name;
    
            archive(age, name);
            std::cout << "Age: " << age << "
    " << "Name: " << name << "
    ";
        }
    }
    
    struct  testxxx
    {
    public :
        int age;
    
        string name;
        
        template<class Archive>
        void serialize(Archive & ar)
        {
            ar(CEREAL_NVP(age));
            ar(CEREAL_NVP(name));
        }
    
        
    };
    
    class MyClass 
    {
    public: //function declarations 
        MyClass( )
        {
             
    
        }
        MyClass(string x,int y,bool z)
        {
            this->x = x;
            this->y = y;
            this->z = z;
            //this->obj1 = obj1;
    
        }
        template<class Archive> // public serialization (normal)
        void serialize(Archive & ar)
        {
            ar(CEREAL_NVP(x));
            ar(CEREAL_NVP(y));
            ar(CEREAL_NVP(z));
            ar(CEREAL_NVP(listobj1));
            //ar(x, y, z,obj1);
        }
    
     // member variables 
        string x;
        int y;
        bool z;
    
        std::vector<testxxx> listobj1;
    };
    
    string Utf8ToGb32(const char * lpszUft8Text)
    {
        int nUnicodeBufLen = MultiByteToWideChar(CP_UTF8, 0, lpszUft8Text, -1, 0, 0);
        if (nUnicodeBufLen == 0)
            return ("");
    
        WCHAR* pUnicodeBuf = new WCHAR[nUnicodeBufLen];
        if (pUnicodeBuf == 0)
            return ("");
    
        MultiByteToWideChar(CP_UTF8, 0, lpszUft8Text, -1, pUnicodeBuf, nUnicodeBufLen);
    
        int nGb32BufLen = WideCharToMultiByte(CP_ACP, 0, pUnicodeBuf, -1, 0, 0, NULL, NULL);
        if (nGb32BufLen == 0)
        {
            delete[] pUnicodeBuf;
            return ("");
        }
    
        char* pGb32Buf = new char[nGb32BufLen];
        if (pGb32Buf == 0)
        {
            delete[] pUnicodeBuf;
            return ("");
        }
    
        WideCharToMultiByte(CP_ACP, 0, pUnicodeBuf, -1, pGb32Buf, nGb32BufLen, NULL, NULL);
    
        string strGb32 = pGb32Buf;
    
        delete[] pUnicodeBuf;
        delete[] pGb32Buf;
    
        return strGb32;
    }
    
    void Serialization_JSON()
    {
        ////string json_str = "";
        //MyClass data("hello", 6, true);
        ////std::stringstream os;
        //{
        //    std::ofstream os("my.json");
        //    cereal::JSONOutputArchive archive_out(os);
        //    archive_out(cereal::make_nvp("MyClass", data));
        //}
        //string json_str = os.str();
        //cout << json_str << endl;
    
        //// deserialize
        ////std::stringstream is(json_str);
        //MyClass data_new;
        //{
        //    std::ifstream is("my.json");
        //    cereal::JSONInputArchive archive_in(is);
        //    //archive_in(data_new);
        //    archive_in(cereal::make_nvp("MyClass", data_new));
        //    //cout << data_new.y << endl;
        //}
    
        //
     
    
     
         //写入json
        {
            std::ofstream os("my.json");
            cereal::JSONOutputArchive archive_out(os);
    
            MyClass data("hello", 6, true);
            testxxx obj1;
            obj1.age = 12;
            obj1.name = "xiao mmksdfsd";
    
            data.listobj1.push_back(obj1);
            obj1.age = 2123;
            obj1.name = "11123xiao mmksdfsd";
            data.listobj1.push_back(obj1);
            archive_out(cereal::make_nvp("MyClass", data));//MyClass 是json中要读取出的名字
        }
    
        //从json中读出
        {
            std::ifstream is("my.json");
            cereal::JSONInputArchive archive_in(is);
     
    
            MyClass data_new;
            archive_in(cereal::make_nvp("MyClass", data_new));
     
            string xxxx = Utf8ToGb32(data_new.x.c_str());//MyClass 是json中要读取出的名字
            
            int cc = 1;
        }
    }
    
    
    void Serialization_Binary()
    {
        {
            std::ofstream os("my.binary", std::ios::binary);
            cereal::BinaryOutputArchive archive(os);
    
            int age = 26;
            std::string name = "lizheng";
    
            archive(CEREAL_NVP(age), CEREAL_NVP(name));
        }
        {
            std::ifstream is("my.binary", std::ios::binary);
            cereal::BinaryInputArchive archive(is);
    
            int age;
            std::string name;
    
            archive(age, name);
            std::cout << "Age: " << age << "
    " << "Name: " << name << "
    ";
        }
    }
    
    void Serialization_Obj()
    {
        {
            std::ofstream os("obj.cereal", std::ios::binary);
            cereal::BinaryOutputArchive archive(os);
    
            MyRecord mr = { 1, 2, 3.0 };
    
            SomeData myData(1111);
            myData.push(100, mr);
    
            archive(myData);
        }
        {
            std::ifstream is("obj.cereal", std::ios::binary);
            cereal::BinaryInputArchive archive(is);
    
            SomeData myData;
            archive(myData);
            myData.print();
        }
    }
    
    
    int main()
    {
        //Serialization_XML();     std::cout << "----------------------
    ";
    
        Serialization_JSON();    std::cout << "----------------------
    ";
    
        //Serialization_Binary();  std::cout << "----------------------
    ";
    
        Serialization_Obj();     std::cout << "----------------------
    ";
    
        getchar();
        return 0;
    }
    json文件:
    {
        "MyClass": {
            "x": "hello",
            "y": 6,
            "z": true,
            "listobj1": [
                {
                    "age": 12,
                    "name": "xiao mmksdfsd"
                },
                {
                    "age": 2123,
                    "name": "11123xiao mmksdfsd"
                }
            ]
        }
    }
    
    
    
    
    
  • 相关阅读:
    MySQL 数据库 查询语句的基本操作,单表查询,多表查询
    MySQL数据库 外键,级联, 修改表的操作
    MySQL数据库 存储引擎,创建表完整的语法,字段类型,约束条件
    MySQL数据库 介绍,安装,基本操作
    python GIL全局解释器锁,多线程多进程效率比较,进程池,协程,TCP服务端实现协程
    子进程回收资源两种方式,僵尸进程与孤儿进程,守护进程,进程间数据隔离,进程互斥锁,队列,IPC机制,线程,守护线程,线程local作用,线程池,回调函数add_done_callback,TCP服务端实现并发
    并发编程 操作系统发展史,多道技术,进程,同步与异步,阻塞与非阻塞,进程的三种状态,创建进程的两种方式
    网络编程 UDP协议 TCP局域网客户端与服务端上传下载电影示例
    网络编程 TCP协议:三次握手,四次挥手,反馈机制 socket套接字通信 粘包问题与解决方法
    k8s 常用命令
  • 原文地址:https://www.cnblogs.com/bleachli/p/10785650.html
Copyright © 2011-2022 走看看