Contents

cereal是一个开源的(BSD License),轻量级的C++序列化库。它只有头文件,支持序列化成binary,xml,JSON。

下面是一个小例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Serialization()
{
{
std::ofstream os("data.xml");
cereal::XMLOutputArchive archive(os);

int Age = 30;
string str = "Dawei";

archive(CEREAL_NVP(Age), cereal::make_nvp("Name", str));
}

{
std::ifstream is("data.xml");
cereal::XMLInputArchive archive(is);

int age;
string name;

archive(age, name);
cout << "Age: " << age << endl << "Name: " << name << endl;
}
}

生成的xml文件如下:
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<cereal>
<Age>30</Age>
<Name>Dawei</Name>
</cereal>

Contents