C/C++实现序列化反序列化

序列化,默认C++ 不支持,所以需要下载Cereal开源库,下载后,配置一下头文件即可正常使用了。

// Test_Console_3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <Windows.h>
#include <cereal/types/memory.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/archives/xml.hpp>
#include <cereal/archives/json.hpp>
#include <fstream>

using namespace std;

// 测试类
class A {
public:
int value1;
string value2;
float value3;
double value4;
char value5[20];

// 构造函数
A(int v1,string v2,float v3,double v4,string v5) {;
value1 = v1;
value2 = v2;
value3 = v3;
value4 = v4;
strcpy(value5, v5.c_str());
}
A(){
value1 = 0;
value2 = "";
value3 = 0;
value4 = 0;
strcpy(value5, "");
}

// 指定哪些成员被序列化
template<class Archive>
void serialize(Archive& archive)
{
archive(value1, value2, value3, value4, value5);
}
};

int main()
{
// “{}” 的作用是刷新内存,否则序列化后必须要等到进程关闭才能将内容写入文件

// XML 序列化
{
std::ofstream os_xml("my.xml");
cereal::XMLOutputArchive archive_xml_1(os_xml);
A a_xml_1(1, "2", 3, 4, "5");
archive_xml_1(
cereal::make_nvp("a_xml.v1", a_xml_1.value1),
cereal::make_nvp("a_xml.v2", a_xml_1.value2),
cereal::make_nvp("a_xml.v3", a_xml_1.value3),
cereal::make_nvp("a_xml.v4", a_xml_1.value4),
cereal::make_nvp("a_xml.v5", a_xml_1.value5)
);
}

// XML 反序列化
{
std::ifstream is_xml("my.xml");
cereal::XMLInputArchive archive_xml_2(is_xml);
A a_xml_2;
archive_xml_2(a_xml_2.value1, a_xml_2.value2, a_xml_2.value3, a_xml_2.value4, a_xml_2.value5);
cout << "a_xml.value1 = " << a_xml_2.value1 << "\n"
<< "a_xml.value2 = " << a_xml_2.value2 << "\n"
<< "a_xml.value3 = " << a_xml_2.value3 << "\n"
<< "a_xml.value4 = " << a_xml_2.value4 << "\n"
<< "a_xml.value5 = " << a_xml_2.value5 << endl;
}

// JSON 序列化
{
std::ofstream os_json("my.json");
cereal::JSONOutputArchive archive_json_1(os_json);
A a_json_1(1, "2", 3, 4, "5");
archive_json_1(
cereal::make_nvp("a_json.v1", a_json_1.value1),
cereal::make_nvp("a_json.v2", a_json_1.value2),
cereal::make_nvp("a_json.v3", a_json_1.value3),
cereal::make_nvp("a_json.v4", a_json_1.value4),
cereal::make_nvp("a_json.v5", a_json_1.value5)
);
}

// JSON 反序列化
{
std::ifstream is_json("my.json");
cereal::JSONInputArchive archive_json_2(is_json);
A a_json_2;
archive_json_2(a_json_2.value1, a_json_2.value2, a_json_2.value3, a_json_2.value4, a_json_2.value5);
cout << "a_json.value1 = " << a_json_2.value1 << "\n"
<< "a_json.value2 = " << a_json_2.value2 << "\n"
<< "a_json.value3 = " << a_json_2.value3 << "\n"
<< "a_json.value4 = " << a_json_2.value4 << "\n"
<< "a_json.value5 = " << a_json_2.value5 << endl;
}

// BIT 序列化
{
std::ofstream os_bit("my.binary", std::ios::binary);
cereal::BinaryOutputArchive archive_bit_1(os_bit);
A a_bit_1(1, "2", 3, 4, "5");
archive_bit_1(
cereal::make_nvp("a_bit.v1", a_bit_1.value1),
cereal::make_nvp("a_bit.v2", a_bit_1.value2),
cereal::make_nvp("a_bit.v3", a_bit_1.value3),
cereal::make_nvp("a_bit.v4", a_bit_1.value4),
cereal::make_nvp("a_bit.v5", a_bit_1.value5)
);
}

// BIT 反序列化
{
std::ifstream is_bit("my.binary", std::ios::binary);
cereal::BinaryInputArchive archive_bit_2(is_bit);
A a_bit_2;
archive_bit_2(a_bit_2.value1, a_bit_2.value2, a_bit_2.value3, a_bit_2.value4, a_bit_2.value5);
cout << "a_bit.value1 = " << a_bit_2.value1 << "\n"
<< "a_bit.value2 = " << a_bit_2.value2 << "\n"
<< "a_bit.value3 = " << a_bit_2.value3 << "\n"
<< "a_bit.value4 = " << a_bit_2.value4 << "\n"
<< "a_bit.value5 = " << a_bit_2.value5 << endl;
}


getchar();
return 0;
}