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 << ")\n";
return os;
} struct SomeData
{
int32_t id;
std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data; SomeData(int32_t id_ = ) : 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(, mr));
} void print()
{
std::cout << "ID : " << id << "\n";
if (data->empty())
return;
for (auto& item : *data)
{
std::cout << item.first << "\t" << item.second << "\n";
}
}
}; void Serialization_XML()
{
{
std::ofstream os("my.xml"); cereal::XMLOutputArchive archive(os); int age = ;
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 << "\n" << "Name: " << name << "\n";
}
} 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, , lpszUft8Text, -, , );
if (nUnicodeBufLen == )
return (""); WCHAR* pUnicodeBuf = new WCHAR[nUnicodeBufLen];
if (pUnicodeBuf == )
return (""); MultiByteToWideChar(CP_UTF8, , lpszUft8Text, -, pUnicodeBuf, nUnicodeBufLen); int nGb32BufLen = WideCharToMultiByte(CP_ACP, , pUnicodeBuf, -, , , NULL, NULL);
if (nGb32BufLen == )
{
delete[] pUnicodeBuf;
return ("");
} char* pGb32Buf = new char[nGb32BufLen];
if (pGb32Buf == )
{
delete[] pUnicodeBuf;
return ("");
} WideCharToMultiByte(CP_ACP, , pUnicodeBuf, -, 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", , true);
testxxx obj1;
obj1.age = ;
obj1.name = "xiao mmksdfsd"; data.listobj1.push_back(obj1);
obj1.age = ;
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 = ;
}
} void Serialization_Binary()
{
{
std::ofstream os("my.binary", std::ios::binary);
cereal::BinaryOutputArchive archive(os); int age = ;
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 << "\n" << "Name: " << name << "\n";
}
} void Serialization_Obj()
{
{
std::ofstream os("obj.cereal", std::ios::binary);
cereal::BinaryOutputArchive archive(os); MyRecord mr = { , , 3.0 }; SomeData myData();
myData.push(, 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 << "----------------------\n"; Serialization_JSON(); std::cout << "----------------------\n"; //Serialization_Binary(); std::cout << "----------------------\n"; Serialization_Obj(); std::cout << "----------------------\n"; getchar();
return ;
}
json文件:
{
"MyClass": {
"x": "hello",
"y": ,
"z": true,
"listobj1": [
{
"age": ,
"name": "xiao mmksdfsd"
},
{
"age": ,
"name": "11123xiao mmksdfsd"
}
]
}
}
USCiLab cereal json 序列化的更多相关文章
- .Net深入实战系列—JSON序列化那点事儿
序 当前主流的序列化JSON字符串主要有两种方式:JavaScriptSerializer及Json.net(Nuget标识:Newtonsoft.Json).JavaScriptSerializer ...
- Newtonsoft.Json 序列化和反序列化 时间格式【转】
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- DotNet的JSON序列化与反序列化
JSON(JavaScript Object Notation)JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.在现在的通信中,较多的采用JSON数据格式,JSON有 ...
- C#中JSON序列化和反序列化
有一段时间没有到博客园写技术博客了,不过每天逛逛博客园中大牛的博客还是有的,学无止境…… 最近在写些调用他人接口的程序,用到了大量的JSON.XML序列化和反序列化,今天就来总结下json的序列化和反 ...
- 使用JSON.Net(Newtonsoft.Json)作为ASP.Net MVC的json序列化和反序列化工具
ASP.Net MVC默认的JSON序列化使用的是微软自己的JavaScriptSerializer.性能低不说,最让人受不了的是Dictionary<,>和Hashtable类型居然对应 ...
- Windows Phone 六、JSON序列化
JSON序列化 public class Person { public int Id { get; set; } public string Name { get; set; } public in ...
- [MVC_Json序列化]MVC之Json序列化循环引用
在做MVC项目时,难免会遇到Json序列化循环引用的问题,大致错误如下 错误1:序列化类型为“...”的对象时检测到循环引用. 错误2:Self referencing loop detected f ...
- NetworkComms V3 使用Json序列化器进行网络通信
刚才在网上闲逛,偶然看到一篇文章 C#(服务器)与Java(客户端)通过Socket传递对象 网址是:http://www.cnblogs.com/iyangyuan/archive/2012/12/ ...
随机推荐
- junit测试
/**ssm框架测试service**/ import com.alibaba.fastjson.JSON; import com.raycloud.waimai.customer.center.po ...
- 这是一个蒟蒻的计划……QAQ
感觉像我这种拖拉的人很有可能是完成不了的,挂上来相当于监督我自己啦QWQ [学习计划] [√]1.去看Trie树!!! yyb学长的blog 2.KMP还有AC自动机 先贴两个链接在这里吧:KMP ...
- docker使用方式
docker使用方式安装:1.安装依赖 yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 2添加yum源 yum-conf ...
- 贪吃蛇游戏——C语言双向链表实现
采用了双向链表结点来模拟蛇身结点: 通过C语言光标控制函数来打印地图.蛇身和食物: /************************** *************************** 贪吃 ...
- 学习笔记-ResNet网络
ResNet网络 ResNet原理和实现 总结 一.ResNet原理和实现 神经网络第一次出现在1998年,当时用5层的全连接网络LetNet实现了手写数字识别,现在这个模型已经是神经网络界的“hel ...
- 2018-2019-2 20165234 《网络对抗技术》 Exp1 PC平台逆向破解
实验一 PC平台逆向破解 实验目的 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另 ...
- [转] 图解Seq2Seq模型、RNN结构、Encoder-Decoder模型 到 Attention
from : https://caicai.science/2018/10/06/attention%E6%80%BB%E8%A7%88/ 一.Seq2Seq 模型 1. 简介 Sequence-to ...
- WPF 10天修炼 第八天 - 形状、画刷和变换
图形 在WPF中使用绘图最简单的就是使用Shape类.Shape类继承自FrameworkElement,是一个专门用来绘图的类.Shape类中年派生的类有直线.矩形.多边形和圆形等. System. ...
- vertx模块DeploymentManager部署管理器
DeploymentManager public DeploymentManager(VertxInternal vertx) { this.vertx = vertx; loadVerticleFa ...
- 论文阅读笔记五十三:Libra R-CNN: Towards Balanced Learning for Object Detection(CVPR2019)
论文原址:https://arxiv.org/pdf/1904.02701.pdf github:https://github.com/OceanPang/Libra_R-CNN 摘要 相比模型的结构 ...