1,关于序列化.png 2,关于序列化.png 3,关于序列化.png 将结构体写入文件 name存在空格,C++的序列化相当的脆弱 C++ 序列化 二进制版 C风格的 序列化,总是正常的 boost 序列化C风格 写入到文件simple boost 序列化C++风格 写入到文件simple boost 序列化从文件中读取 非侵入式的 boost序列化 缺点:所有的成员必须是public 可序列化的成员 派生类的序列化,问题引出,父类的成员增删对序列化有影响.耦合度太高 派生类的序列化 问题解决 指针的序列化 两个指针指向同一片区域,序列化到文件,只保存一份 智能指针的序列化 数组的序列化 指针数组的序列化 STL容器的序列化 将对象序列化为XML描述
将结构体写入文件
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分开,方便stream的读入 return os<<user.name<<" "<<user.email<<" "<<user.age; } istream& operator>>(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.txt");//为了演示,省略检查机制 fout<< user1 <<endl;//写入到文件 fout<< user2 <<endl;//写入到文件 User user3; User user4; ifstream fin("user.txt"); fin >> user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ > user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out chunli li_chunli@mail.com 24 林冲 林冲@email.com 30 chunli@Linux:~/boost$ cat user.txt chunli li_chunli@mail.com 24 林冲 林冲@email.com 30 chunli@Linux:~/boost$
name存在空格,C++的序列化相当的脆弱
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; ostream& operator<<(ostream& os,const User& user ) { //空格分开,方便stream的读入 return os<<user.name<<" "<<user.email<<" "<<user.age; } istream& operator>>(istream& is,User& user ) { return is>>user.name>>user.email>>user.age; } int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.txt");//为了演示,省略检查机制 fout<< user1 <<endl;//写入到文件 fout<< user2 <<endl;//写入到文件 User user3; User user4; ifstream fin("user.txt"); fin >> user3; fin >> user4; cout << user3 << endl; cout << user4 << endl; return 0; } chunli@Linux:~/boost$ >user.txt chunli@Linux:~/boost$ g++ main.cpp && ./a.out li chunli 0 335933440 chunli@Linux:~/boost$ cat user.txt li chunli li_chunli@mail.com 24 林冲 林冲@email.com 30 chunli@Linux:~/boost$
C++ 序列化 二进制版
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { string name; string email; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.bin");//为了演示,省略检查机制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; User user4; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.close(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -Wall -g && ./a.out li chunli,li_chunli@mail.com,24 林冲,林冲@email.com,30 *** Error in `./a.out‘: double free or corruption (fasttop): 0x00000000008a90b0 *** 已放弃 (核心已转储) chunli@Linux:~/boost$
C风格的 序列化,总是正常的
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> using namespace std; struct User { char name[100]; char email[100]; int age; }; int main() { User user1 = {"li chunli","li_chunli@mail.com",24}; User user2 = {"林冲","林冲@email.com",30}; ofstream fout("user.bin");//为了演示,省略检查机制 fout.write((const char*)&user1,sizeof(struct User)); fout.write((const char*)&user2,sizeof(struct User)); fout.close(); User user3; ifstream fin("user.bin"); fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.read((char*)&user3,sizeof(struct User)); cout << user3.name << "," <<user3.email <<","<<user3.age<< endl; fin.close(); return 0; } chunli@Linux:~/boost$ g++ main.cpp -Wall -g && ./a.out li chunli,li_chunli@mail.com,24 林冲,林冲@email.com,30 chunli@Linux:~/boost$
boost 序列化C风格 写入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { char name[16]; char email[64]; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 16 -26 -103 -70 -27 -92 -102 -26 -104 -97 0 0 0 0 0 0 0 64 -27 -112 -76 -25 -108 -88 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40 16 -24 -79 -71 -27 -83 -112 -27 -92 -76 0 0 0 0 0 0 0 64 -26 -98 -105 -27 -122 -78 64 101 109 97 105 108 46 99 111 109 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 chunli@Linux:~/boost$
boost 序列化C++风格 写入到文件simple
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1; oa << user2; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吴用@email.com 40 9 豹子头 16 林冲@email.com 30 chunli@Linux:~/boost$
boost 序列化从文件中读取
chunli@Linux:~/boost$ >user_info chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name;//ar & name & email & age; ar & email; ar & age; } }; int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星,吴用@email.com,40 豹子头,林冲@email.com,30 chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吴用@email.com 40 9 豹子头 16 林冲@email.com 30chunli@Linux:~/boost$
非侵入式的 boost序列化 缺点:所有的成员必须是public
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; }; template<class Archive> void serialize(Archive &ar,User &user ,const unsigned version) { ar & user.name & user.email & user.age; } int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; ofstream fout("user_info");//为了演示,省略检查机制 boost::archive::text_oarchive oa(fout); oa << user1 << user2; fout.close(); User user_tmp; ifstream fin("user_info"); boost::archive::text_iarchive ia(fin); ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; ia>>user_tmp; cout <<user_tmp.name <<","<<user_tmp.email<<","<<user_tmp.age<<endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星,吴用@email.com,40 豹子头,林冲@email.com,30 chunli@Linux:~/boost$ cat user_info 22 serialization::archive 14 0 0 9 智多星 16 吴用@email.com 40 9 豹子头 16 林冲@email.com 30chunli@Linux:~/boost$ chunli@Linux:~/boost$
可序列化的成员
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct Group { string gid; User leader; vector<User> members; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & gid & leader & members; } }; ostream& operator<<(ostream& os,const User& user) { return os<<user.name<<", "<<user.email<<", "<<user.age<<endl; } int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; User user3 = {"及时雨","宋江@email.com",42}; User user4 = {"黑旋风","李逵@email.com",37}; Group group; group.gid = "8080"; group.leader = user1; group.members.push_back(user2); group.members.push_back(user3); group.members.push_back(user4); //写入到文件 ofstream fout("group_info"); boost::archive::text_oarchive oa(fout); oa << group; fout.close(); //从文件中读取 Group group2; ifstream fin("group_info"); boost::archive::text_iarchive ia(fin); ia >> group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator<User>(cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator<User> (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吴用@email.com, 40 豹子头, 林冲@email.com, 30 及时雨, 宋江@email.com, 42 黑旋风, 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吴用@email.com 40 0 0 3 0 9 豹子头 16 林冲@email.com 30 9 及时雨 16 宋江@email.com 42 9 黑旋风 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
派生类的序列化,问题引出,父类的成员增删对序列化有影响.耦合度太高
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct VIP:public User { int level; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & email;//将成员序列化到IO ar & level; ar & name; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋风"; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋风 likui@mail.com chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 14 likui@mail.com 100 9 黑旋风chunli@Linux:~/boost$ chunli@Linux:~/boost$
派生类的序列化 问题解决
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct VIP:public User { int level; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & boost::serialization::base_object<User>(*this);//将父类序列化,不再管父类有多少个成员 ar & level; } }; int main() { VIP vip1; vip1.level = 100; vip1.name = "黑旋风"; vip1.age = 34; vip1.email = "likui@mail.com"; ofstream fout("users_vip"); boost::archive::text_oarchive oa(fout); oa << vip1; fout.close(); VIP vip2; ifstream fin("users_vip"); boost::archive::text_iarchive ia(fin); ia >> vip2; cout << vip2.level << endl; cout << vip2.name << endl; cout << vip2.email << endl; cout << vip2.age << endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 100 黑旋风 likui@mail.com 34 chunli@Linux:~/boost$ cat users_vip 22 serialization::archive 14 0 0 0 0 9 黑旋风 14 likui@mail.com 34 100chunli@Linux:~/boost$ chunli@Linux:~/boost$
指针的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及时雨","songjiang@mail.com",37); ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; file.close(); delete user1; User *user2; ifstream fin("user.info"); //boost::archive::text_iarchive ia(fin); boost::archive::text_iarchive ia(fin); ia >> user2; cout <<"name="<<user2->getName()<<", email="<<user2->getEmail()<<", age="<< user2->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及时雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及时雨 18 songjiang@mail.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
两个指针指向同一片区域,序列化到文件,只保存一份
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及时雨","songjiang@mail.com",37); User *user2 = user1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; file.close(); delete user1; User *user3; User *user4; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; ia >> user4; cout <<"name="<<user4->getName()<<", email="<<user4->getEmail()<<", age="<< user4->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及时雨, email=songjiang@mail.com, age=37 name=及时雨, email=songjiang@mail.com, age=37 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及时雨 18 songjiang@mail.com 37 0 0chunli@Linux:~/boost$ chunli@Linux:~/boost$ 问题:以指针序列化,以对象读取,失败
智能指针的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *user1 = new User("及时雨","songjiang@mail.com",37); User *user2 = user1; boost::shared_ptr<User> sp1(new User("花和尚", "鲁智深@mail.com", 38)); boost::shared_ptr<User> sp2 = sp1; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << user1; oa << user2; oa << sp1; oa << sp2; file.close(); delete user1; User *user3; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; ia >> user3; cout <<"name="<<user3->getName()<<", email="<<user3->getEmail()<<", age="<< user3->getAge()<< endl; boost::shared_ptr<User> sp3; ia >> sp3; cout <<"name="<<sp3->getName()<<", email="<<sp3->getEmail()<<", age="<< sp3->getAge()<< endl; ia >> sp3; cout <<"name="<<sp3->getName()<<", email="<<sp3->getEmail()<<", age="<< sp3->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out name=及时雨, email=songjiang@mail.com, age=37 name=及时雨, email=songjiang@mail.com, age=37 name=花和尚, email=鲁智深@mail.com, age=38 name=花和尚, email=鲁智深@mail.com, age=38 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 0 1 0 0 9 及时雨 18 songjiang@mail.com 37 0 0 0 1 0 1 9 花和尚 18 鲁智深@mail.com 38 0 1chunli@Linux:~/boost$ chunli@Linux:~/boost$
数组的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User users[3] = { User("及时雨","songjiang@mail.com",37), User("武行者","wusong@mail.com",37), User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout <<buf[0].getName()<<","<<buf[0].getEmail()<<","<< buf[0].getAge()<< endl; cout <<buf[1].getName()<<","<<buf[1].getEmail()<<","<< buf[1].getAge()<< endl; cout <<buf[2].getName()<<","<<buf[2].getEmail()<<","<< buf[2].getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out 及时雨,songjiang@mail.com,37 武行者,wusong@mail.com,37 花和尚,luzhishen@mail.com,40 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 3 0 0 9 及时雨 18 songjiang@mail.com 37 9 武行者 15 wusong@mail.com 37 9 花和尚 18 luzhishen@mail.com 40chunli@Linux:~/boost$ chunli@Linux:~/boost$
指针数组的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/shared_ptr.hpp> using namespace std; class User { public: User():name(),email(),age() { } User(const string& _name,const string& _email,const int & _age):name(_name),email(_email),age(_age) { } string getName()const //函数体内不能对成员数据做任何改动 { return name; } string getEmail()const //函数体内不能对成员数据做任何改动 { return email; } int getAge()const //函数体内不能对成员数据做任何改动 { return age; } private: string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; int main() { User *users[3] = { new User("及时雨","songjiang@mail.com",37), new User("武行者","wusong@mail.com",37), new User("花和尚","luzhishen@mail.com",40) }; ofstream file("user.info"); boost::archive::text_oarchive oa(file); oa << users; file.close(); User *buf[3]; ifstream fin("user.info"); boost::archive::text_iarchive ia(fin); ia >> buf; cout <<buf[0]->getName()<<","<<buf[0]->getEmail()<<","<< buf[0]->getAge()<< endl; cout <<buf[1]->getName()<<","<<buf[1]->getEmail()<<","<< buf[1]->getAge()<< endl; cout <<buf[2]->getName()<<","<<buf[2]->getEmail()<<","<< buf[2]->getAge()<< endl; return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out 及时雨,songjiang@mail.com,37 武行者,wusong@mail.com,37 花和尚,luzhishen@mail.com,40 chunli@Linux:~/boost$ cat user.info 22 serialization::archive 14 3 0 1 0 0 9 及时雨 18 songjiang@mail.com 37 0 1 9 武行者 15 wusong@mail.com 37 0 2 9 花和尚 18 luzhishen@mail.com 40chunli@Linux:~/boost$ chunli@Linux:~/boost$
STL容器的序列化
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/serialization/vector.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & name & email & age; } }; struct Group { string gid; User leader; vector<User> members; friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar,const unsigned version) { ar & gid & leader & members; } }; ostream& operator<<(ostream& os,const User& user) { return os<<user.name<<", "<<user.email<<", "<<user.age<<endl; } int main() { User user1 = {"智多星","吴用@email.com",40}; User user2 = {"豹子头","林冲@email.com",30}; User user3 = {"及时雨","宋江@email.com",42}; User user4 = {"黑旋风","李逵@email.com",37}; Group group; group.gid = "8080"; group.leader = user1; group.members.push_back(user2); group.members.push_back(user3); group.members.push_back(user4); //写入到文件 ofstream fout("group_info"); boost::archive::text_oarchive oa(fout); oa << group; fout.close(); //从文件中读取 Group group2; ifstream fin("group_info"); boost::archive::text_iarchive ia(fin); ia >> group2; cout << group2.leader ; //copy(group2.members.begin(),group2.members.end(),ostream_iterator<User>(cout," OK")); copy(group2.members.begin(),group2.members.end(),ostream_iterator<User> (cout)); return 0; } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization -Wall && ./a.out 智多星, 吴用@email.com, 40 豹子头, 林冲@email.com, 30 及时雨, 宋江@email.com, 42 黑旋风, 李逵@email.com, 37 chunli@Linux:~/boost$ cat group_info 22 serialization::archive 14 0 0 4 8080 0 0 9 智多星 16 吴用@email.com 40 0 0 3 0 9 豹子头 16 林冲@email.com 30 9 及时雨 16 宋江@email.com 42 9 黑旋风 16 李逵@email.com 37chunli@Linux:~/boost$ chunli@Linux:~/boost$
将对象序列化为XML描述
chunli@Linux:~/boost$ cat main.cpp #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <iterator> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/serialization/vector.hpp> using namespace std; struct User { string name; string email; int age; friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned version) { ar & BOOST_SERIALIZATION_NVP(name); ar & BOOST_SERIALIZATION_NVP(email); ar & BOOST_SERIALIZATION_NVP(age); } }; ostream& operator<<(ostream& os, const User& u) { return os << u.name << ‘ ‘ << u.email << ‘ ‘ << u.age; } struct Group { string gid; User leader; vector<User> members; friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned version) { ar & BOOST_SERIALIZATION_NVP(gid); ar & BOOST_SERIALIZATION_NVP(leader); ar & BOOST_SERIALIZATION_NVP(members); } }; int main() { Group group; group.gid = "G01"; User leader = { "tom", "tom@abc.com", 28 }; group.leader = leader; User u1 = { "tom1", "tom1@abc.com", 28 }; User u2 = { "tom2", "tom2@abc.com", 28 }; User u3 = { "tom3", "tom3@abc.com", 28 }; group.members.push_back(u1); group.members.push_back(u2); group.members.push_back(u3); ofstream fout("users_xml"); boost::archive::xml_oarchive oa(fout); oa << BOOST_SERIALIZATION_NVP(group); fout.close(); Group g2; ifstream fin("users_xml"); boost::archive::xml_iarchive ia(fin); ia >> BOOST_SERIALIZATION_NVP(g2); cout << g2.leader << endl; copy(g2.members.begin(), g2.members.end(), ostream_iterator<User> (cout,"\n")); } chunli@Linux:~/boost$ g++ main.cpp -l boost_serialization && ./a.out tom tom@abc.com 28 tom1 tom1@abc.com 28 tom2 tom2@abc.com 28 tom3 tom3@abc.com 28 chunli@Linux:~/boost$ cat users_xml <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="14"> <group class_id="0" tracking_level="0" version="0"> <gid>G01</gid> <leader class_id="1" tracking_level="0" version="0"> <name>tom</name> <email>tom@abc.com</email> <age>28</age> </leader> <members class_id="2" tracking_level="0" version="0"> <count>3</count> <item_version>0</item_version> <item> <name>tom1</name> <email>tom1@abc.com</email> <age>28</age> </item> <item> <name>tom2</name> <email>tom2@abc.com</email> <age>28</age> </item> <item> <name>tom3</name> <email>tom3@abc.com</email> <age>28</age> </item> </members> </group> chunli@Linux:~/boost$ chunli@Linux:~/boost$
本文出自 “魂斗罗” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1884844
原文地址:http://990487026.blog.51cto.com/10133282/1884844