轻量级序列化库boost serialization

6 篇文章 0 订阅

对象序列化是项目中最经常看到的场景,因此实现该功能的库也有很多,如:thrift、protobuf、avro,但我觉得这些库都太过沉重,因为他们还提供了rpc的功能,如果只是单纯做对象序列化,boost serialization是个不错的选择,以下boost serialization简称BS。

一、代码目录

1.$BOOST_HOME/archive存档方式类目录

类型普通字节宽字节
二进制binary_iarchive.hpp
binary_oarchive.hpp
binary_wiarchive.hpp
binary_woarchive.hpp
文本text_iarchive.hpp
text_oarchive.hpp
text_wiarchive.hpp
text_woarchive.hpp
xmlxml_iarchive.hpp
xml_oarchive.hpp
xml_wiarchive.hpp
xml_woarchive.hpp

2.$BOOST_HOME/serialization 序列化数据类型目录

目前BS基本上几乎支持了所有的C++数据类型,还包括指针、数组等,并且也支持了stl的容器,但要包含相关头文件。

vector.hpp支持vector的序列化

list.hpp支持list的序列化

deque.hpp支持deque的序列化

map.hpp支持map的序列化

hash_map.hpp支持hash_map的序列化

等等...

除了stl的容器,也支持boost的相关容器,如array.hpp


二、序列化类实现

1.置入式实现

  1. #include <fstream>   
  2.   
  3. // 文本方式存档   
  4. #include <boost/archive/text_oarchive.hpp>   
  5. #include <boost/archive/text_iarchive.hpp>   
  6.   
  7. /   
  8. // gps coordinate   
  9. //   
  10. // illustrates serialization for a simple type   
  11. //   
  12. class gps_position  
  13. {  
  14. private:  
  15.     friend class boost::serialization::access;//置入式   
  16.     template<class Archive>  
  17.     void serialize(Archive & ar, const unsigned int version)  
  18.     {  
  19.         ar & degrees;//序列化或反序列化&操作符比>>和<<更为方便   
  20.         ar & minutes;  
  21.         ar & seconds;  
  22.     }  
  23.     int degrees;  
  24.     int minutes;  
  25.     float seconds;  
  26. public:  
  27.     gps_position(){};  
  28.     gps_position(int d, int m, float s) :  
  29.         degrees(d), minutes(m), seconds(s)  
  30.     {}  
  31. };  
  32.   
  33. int main() {  
  34.     std::fstream ofs("filename");  
  35.   
  36.     // create class instance   
  37.     const gps_position g(35, 59, 24.567f);  
  38.   
  39.     // save data to archive   
  40.     {  
  41.         boost::archive::text_oarchive oa(ofs);  
  42.         oa << g;  
  43.     }  
  44.   
  45.     // ... some time later restore the class instance to its orginal state   
  46.     gps_position newg;  
  47.     {  
  48.         std::fstream ifs("filename");  
  49.         boost::archive::text_iarchive ia(ifs);  
  50.         ia >> newg;  
  51.     }  
  52.     return 0;  
  53. }  
2.非置入实现

  1. #include <boost/archive/text_oarchive.hpp>   
  2. #include <boost/archive/text_iarchive.hpp>   
  3.   
  4. class gps_position  
  5. {  
  6. public:  
  7.     int degrees;  
  8.     int minutes;  
  9.     float seconds;  
  10.     gps_position(){};  
  11.     gps_position(int d, int m, float s) :  
  12.         degrees(d), minutes(m), seconds(s)  
  13.     {}  
  14. };  
  15.   
  16. namespace boost {  
  17. namespace serialization {  
  18.   
  19. template<class Archive>  
  20. void serialize(Archive & ar, gps_position & g, const unsigned int version)//必须包含在boost::serialization名字空间下   
  21. {  
  22.     ar & g.degrees;  
  23.     ar & g.minutes;  
  24.     ar & g.seconds;  
  25. }  
  26.   
  27. // namespace serialization   
  28. // namespace boost  

3.可以序列化类对象,但被序列化的类必须实现序列化函数

  1. class bus_stop  
  2. {  
  3.     friend class boost::serialization::access;  
  4.     template<class Archive>  
  5.     void serialize(Archive & ar, const unsigned int version)  
  6.     {  
  7.         ar & latitude;  
  8.         ar & longitude;  
  9.     }  
  10.     gps_position latitude;  
  11.     gps_position longitude;  
  12. protected:  
  13.     bus_stop(const gps_position & lat_, const gps_position & long_) :  
  14.     latitude(lat_), longitude(long_)  
  15.     {}  
  16. public:  
  17.     bus_stop(){}  
  18.     // See item # 14 in Effective C++ by Scott Meyers.   
  19.     // re non-virtual destructors in base classes.   
  20.     virtual ~bus_stop(){}  
  21. };  

4.继承关系的序列化

  1. #include <boost/serialization/base_object.hpp>//必须包含这个头文件   
  2.   
  3. class bus_stop_corner : public bus_stop  
  4. {  
  5.     friend class boost::serialization::access;  
  6.     template<class Archive>  
  7.     void serialize(Archive & ar, const unsigned int version)  
  8.     {  
  9.         // serialize base class information   
  10.         ar & boost::serialization::base_object<bus_stop>(*this);//序列化基类   
  11.         ar & street1;  
  12.         ar & street2;  
  13.     }  
  14.     std::string street1;  
  15.     std::string street2;  
  16.     virtual std::string description() const  
  17.     {  
  18.         return street1 + " and " + street2;  
  19.     }  
  20. public:  
  21.     bus_stop_corner(){}  
  22.     bus_stop_corner(const gps_position & lat_, const gps_position & long_,  
  23.         const std::string & s1_, const std::string & s2_  
  24.     ) :  
  25.         bus_stop(lat_, long_), street1(s1_), street2(s2_)  
  26.     {}  
  27. };  

5.序列化数组

  1. class bus_route  
  2. {  
  3.     friend class boost::serialization::access;  
  4.     bus_stop stops[10];  
  5.     template<class Archive>  
  6.     void serialize(Archive & ar, const unsigned int version)  
  7.     {  
  8.         ar & stops;//会自动在前面加上数组的长度   
  9.     }  
  10. public:  
  11.     bus_route(){}  
  12. };  

6.序列化stl容器

  1. #include <boost/serialization/list.hpp>   
  2.   
  3. class bus_route  
  4. {  
  5.     friend class boost::serialization::access;  
  6.     std::list<bus_stop> stops;  
  7.     template<class Archive>  
  8.     void serialize(Archive & ar, const unsigned int version)  
  9.     {  
  10.         ar & stops;//会自动加上容器的长度   
  11.     }  
  12. public:  
  13.     bus_route(){}  
  14. };  

7.可根据版本号序列化

  1. #include <boost/serialization/list.hpp>   
  2. #include <boost/serialization/string.hpp>   
  3. #include <boost/serialization/version.hpp> //必须包含这个头文件   
  4.   
  5. class bus_route  
  6. {  
  7.     friend class boost::serialization::access;  
  8.     std::list<bus_stop> stops;  
  9.     std::string driver_name;  
  10.     template<class Archive>  
  11.     void serialize(Archive & ar, const unsigned int version)  
  12.     {  
  13.         // only save/load driver_name for newer archives   
  14.         if(version > 0)//根据版本号序列化   
  15.             ar & driver_name;  
  16.         ar & stops;  
  17.     }  
  18. public:  
  19.     bus_route(){}  
  20. };  
  21.   
  22. BOOST_CLASS_VERSION(bus_route, 1) //定义version=1  

8.读写分离

一直以来都是serialize这个函数做序列化很反序列化,但有时候序列化和反序列化的方法不一致,serialize函数就难以支持了,因此要用save/load函数,save做序列化操作,而load做反序列化操作。

  1. #include <boost/serialization/list.hpp>   
  2. #include <boost/serialization/string.hpp>   
  3. #include <boost/serialization/version.hpp>   
  4. #include <boost/serialization/split_member.hpp>   
  5.   
  6. class bus_route  
  7. {  
  8.     friend class boost::serialization::access;  
  9.     std::list<bus_stop> stops;  
  10.     std::string driver_name;  
  11.     template<class Archive>  
  12.     void save(Archive & ar, const unsigned int version) const  
  13.     {  
  14.         // note, version is always the latest when saving   
  15.         ar  & driver_name;  
  16.         ar  & stops;  
  17.     }  
  18.     template<class Archive>  
  19.     void load(Archive & ar, const unsigned int version)  
  20.     {  
  21.         if(version > 0)  
  22.             ar & driver_name;  
  23.         ar  & stops;  
  24.     }  
  25.     BOOST_SERIALIZATION_SPLIT_MEMBER() //这个宏定义必须要在类中加上   
  26. public:  
  27.     bus_route(){}  
  28. };  
  29.   
  30. BOOST_CLASS_VERSION(bus_route, 1)  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值