下面是struct二进制方式序列化,包含vector类型序列化
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/export.hpp>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
typedef struct node
{
string text;
node():text("node text")
{
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & text;
}
}node;
typedef struct date
{
unsigned int m_day;
unsigned int m_month;
string m_year;
vector<node> node1;
//node node1;
date()
{
}
date( int d, int m, string y) : m_day(d), m_month(m), m_year(y)
{
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & m_day;
ar & m_month;
ar & m_year;
ar & node1;
}
} date;
int main()
{
date dr(2,6,"2016");
vector<node> vec;
node n1;
n1.text = "aa";
node n2;
n2.text = "bb";
node n3;
n3.text = "cc";
vec.push_back(n1);
vec.push_back(n2);
vec.push_back(n3);
dr.node1 = vec;
// dr.node1.text = "aaa";
ostringstream os;
boost::archive::binary_oarchive oa(os);
oa << dr;
string content = os.str();
cout << "ss:" << content << endl;
date d2;
std::istringstream is(content);
boost::archive::binary_iarchive ia(is);
ia >> d2;//从一个保存序列化数据的string里面反序列化,从而得到原来的对象。
std::cout << "date:day:" << d2.m_day << ", month: " << d2.m_month << ",year:" << d2.m_year << endl;
vector<node> vv = d2.node1;
for(int i = 0; i < vv.size(); i++)
{
cout << "vec1:" << vv[i].text << endl;
}
}
下面是二进制序列化类的示例,包含数组,指针,几类
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/export.hpp>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
class A
{
public:
A(){}
virtual ~A(){
}
public:
int a;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & a;
}
};
class B : public A
{
public:
B(){
}
public:
int b;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & boost::serialization::base_object<A>(*this);
ar & b;
}
};
BOOST_CLASS_EXPORT_GUID(B, "B")
class C
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & obj;
}
public:
C(){
}
public:
A* obj[3];
};
int main()
{
A a1;
a1.a = 1;
B b1;
b1.a = 11;
b1.b = 12;
C c1;
c1.obj[0] = &a1;
c1.obj[1] = &b1;
c1.obj[2] = &a1;
std::ostringstream os;
boost::archive::binary_oarchive oa(os);
oa << c1;
string content = os.str();
C c2;
std::istringstream is(content);
boost::archive::binary_iarchive ia(is);
ia >> c2;
for (int i = 0; i < 3; i++)
{
A* d = c2.obj[i];
if (i == 1)
{
B* b = reinterpret_cast<B*>(d);
std::cout << "pointer" << i + 1 <<", a: " << b->a << ", b:" << b->b<< endl;
}
else
{
std::cout << "pointer" << i + 1 <<": " << d->a << endl;
}
}
}