cereal:C++实现的开源序列化库
作者:网络转载 发布时间:[ 2014/6/11 11:42:13 ] 推荐标签:C++ net
我的Demo
完整代码如下(或点此下载完整工程,或者从我的github下载包括cereal头文件在内的整个项目):
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include "cereal/archives/binary.hpp"
5 #include "cereal/archives/xml.hpp"
6 #include "cereal/archives/json.hpp"
7 #include "cereal/types/unordered_map.hpp"
8 #include "cereal/types/memory.hpp"
9 #include "cereal/types/string.hpp" //一定要包含此文件,否则无法将std::string序列化为二进制形式,请看:https://github.com/USCiLab/cereal/issues/58
10
11 using namespace std;
12
13 struct MyRecord
14 {
15 int x, y;
16 float z;
17
18 template <class Archive>
19 void serialize(Archive & ar)
20 {
21 ar(x, y, z);
22 }
23
24 friend std::ostream& operator<<(std::ostream& os, const MyRecord& mr);
25 };
26
27 std::ostream& operator<<(std::ostream& os, const MyRecord& mr)
28 {
29 os << "MyRecord(" << mr.x << ", " << mr.y << "," << mr.z << ")
";
30 return os;
31 }
32
33 struct SomeData
34 {
35 int32_t id;
36 std::shared_ptr<std::unordered_map<uint32_t, MyRecord>> data;
37
38 SomeData(int32_t id_=0) : id(id_), data(new std::unordered_map<uint32_t, MyRecord>)
39 {
40
41 }
42
43 template <class Archive>
44 void save(Archive & ar) const
45 {
46 ar(id, data);
47 }
48
49 template <class Archive>
50 void load(Archive & ar)
51 {
52 ar(id, data);
53 }
54
55 void push(uint32_t, const MyRecord& mr)
56 {
57 data->insert(std::make_pair(100, mr));
58 }
59
60 void print()
61 {
62 std::cout << "ID : " << id << "
";
63 if (data->empty())
64 return;
65 for (auto& item : *data)
66 {
67 std::cout << item.first << " " << item.second << "
";
68 }
69 }
70 };
71
72 void Serialization_XML()
73 {
74 {
75 std::ofstream os("my.xml");
76
77 cereal::XMLOutputArchive archive(os);
78
79 int age = 26;
80 std::string name = "lizheng";
81
82 //#define CEREAL_NVP(T) ::cereal::make_nvp(#T, T)
83 archive(CEREAL_NVP(age), cereal::make_nvp("Name", name));
84
85 //os.close(); //注意:这里不能显示关闭ofstream,否则序列化无法写入到文件
86 }
87
88 {
89 std::ifstream is("my.xml");
90 cereal::XMLInputArchive archive(is);
91
92 int age;
93 std::string name;
94
95 archive(age, name);
96 std::cout << "Age: " << age << "
" << "Name: " << name << "
";
97 }
98 }
99
100 void Serialization_JSON()
101 {
102 {
103 std::ofstream os("my.json");
104 cereal::JSONOutputArchive archive(os);
105
106 int age = 26;
107 std::string name = "lizheng";
108
109 archive(CEREAL_NVP(age), cereal::make_nvp("Name", name));
110 }
111
112 {
113 std::ifstream is("my.json");
114 cereal::JSONInputArchive archive(is);
115
116 int age;
117 std::string name;
118
119 archive(age, name);
120 std::cout << "Age: " << age << "
" << "Name: " << name << "
";
121 }
122 }
123
124
125 void Serialization_Binary()
126 {
127 {
128 std::ofstream os("my.binary", std::ios::binary);
129 cereal::BinaryOutputArchive archive(os);
130
131 int age = 26;
132 std::string name = "lizheng";
133
134 archive(CEREAL_NVP(age), CEREAL_NVP(name));
135 }
136 {
137 std::ifstream is("my.binary", std::ios::binary);
138 cereal::BinaryInputArchive archive(is);
139
140 int age;
141 std::string name;
142
143 archive(age, name);
144 std::cout << "Age: " << age << "
" << "Name: " << name << "
";
145 }
146 }
147
148 void Serialization_Obj()
149 {
150 {
151 std::ofstream os("obj.cereal", std::ios::binary);
152 cereal::BinaryOutputArchive archive(os);
153
154 MyRecord mr = { 1, 2, 3.0 };
155
156 SomeData myData(1111);
157 myData.push(100, mr);
158
159 archive(myData);
160 }
161 {
162 std::ifstream is("obj.cereal", std::ios::binary);
163 cereal::BinaryInputArchive archive(is);
164
165 SomeData myData;
166 archive(myData);
167 myData.print();
168 }
169 }
170
171
172 int main()
173 {
174 Serialization_XML(); std::cout << "----------------------
";
175
176 Serialization_JSON(); std::cout << "----------------------
";
177
178 Serialization_Binary(); std::cout << "----------------------
";
179
180 Serialization_Obj(); std::cout << "----------------------
";
181
182 getchar();
183 return 0;
184 }
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南