Object serialization is the most frequently seen scenario in a project, so there are a number of libraries that implement this functionality, such as thrift, PROTOBUF, Avro, but I think these libraries are too heavy because they also provide RPC functionality, and if you simply do object serialization, boost Serialization is a good choice, the following boost serialization abbreviation BS. A, code directory 1. $BOOST _home/archive Archiving Method class directory
| type |
Normal byte |
Wide byte |
| Binary system |
Binary_iarchive.hpp Binary_oarchive.hpp |
Binary_wiarchive.hpp Binary_woarchive.hpp |
| Text |
Text_iarchive.hpp Text_oarchive.hpp |
Text_wiarchive.hpp Text_woarchive.hpp |
| Xml |
Xml_iarchive.hpp Xml_oarchive.hpp |
Xml_wiarchive.hpp Xml_woarchive.hpp |
2. $BOOST _home/serialization serialization Data type directory
At present, BS almost support all C + + data types, including pointers, arrays, etc., and also support the STL container, but to include related header files.
VECTOR.HPP supports the serialization of vectors
Serialization of LIST.HPP Support list
DEQUE.HPP Support for Deque serialization
MAP.HPP supports the serialization of maps
HASH_MAP.HPP Support for HASH_MAP serialization
Wait a minute...
In addition to STL containers, boost-related containers are also supported, such as ARRAY.HPP
Second, the serialization of class implementation 1. The placement of the implementation
#include <fstream>//Text archive #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_ Iarchive.hpp>///////////////////////////////////////////////////////////////GPS coordinate////illustrates Serialization for-a simple type//class Gps_position {Private:friend class boost::serialization::access;//placement T Emplate<class archive> void serialize (Archive & AR, const unsigned int version) {AR & Degre
es;//serialization or deserialization & operators are more convenient than >> and << ar & minutes;
Ar & seconds;
int degrees;
int minutes;
float seconds;
Public:gps_position () {};
gps_position (int d, int m, float s): degrees (d), minutes (m), seconds (s) {}};
int main () {Std::fstream OFS ("filename");
Create class Instance const Gps_position g (24.567f);
Save data to archive {boost::archive::text_oarchive OA (OFS);
OA << G; }
// ... Some time later restore the class instance to its orginal state gps_position NEWG;
{Std::fstream IFS ("filename");
Boost::archive::text_iarchive IA (IFS);
IA >> NEWG;
return 0; }
2. Non-placement Implementation
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
class Gps_position
{public
:
int degrees;
int minutes;
float seconds;
Gps_position () {};
gps_position (int d, int m, float s):
degrees (d), minutes (m), seconds (s)
{}
};
Namespace boost {
namespace serialization {
Template<class archive>
void Serialize (Archive & AR, Gps_position & G, const unsigned int version)//must be included under the Boost::serialization namespace
{
ar & g.degrees;< C19/>ar & g.minutes;
Ar & g.seconds;
}
} namespace serialization
}//Namespace boost
3. A class object can be serialized, but a serialized class must implement a serialization function
Class Bus_stop
{
friend class boost::serialization::access;
Template<class archive>
void serialize (Archive & AR, const unsigned int version)
{
ar & Latitude;
Ar & longitude;
}
Gps_position latitude;
Gps_position longitude;
Protected:
bus_stop (const gps_position & lat_, const gps_position & long_):
latitude (lat_), Longitude (Long_)
{} public
:
bus_stop () {}
//Check Item # in effective C + + by Scott Meyers.
Re non-virtual destructors in base classes.
Virtual ~bus_stop () {}
};
4. Serialization of inheritance Relationships
#include <boost/serialization/base_object.hpp>//must contain this header file
class Bus_stop_corner:public bus_stop
{
friend class boost::serialization::access;
Template<class archive>
void serialize (Archive & AR, const unsigned int version)
{
//Serialize base class Information
ar & boost::serialization::base_object<bus_stop> (*this);//serialization of base class
AR & Street1;
Ar & Street2;
}
std::string Street1;
std::string Street2;
Virtual std::string description () const
{return
Street1 + "and" + Street2;
}
Public:
Bus_stop_corner () {}
Bus_stop_corner (const gps_position & lat_, const gps_position & long_,< C20/>const std::string & s1_, const std::string & s2_
):
bus_stop (Lat_, Long_), Street1 (s1_), Street2 ( s2_)
{}
};
5. Serialization of Arrays
Class Bus_route
{
friend class boost::serialization::access;
Bus_stop stops[10];
Template<class archive>
void serialize (Archive & AR, const unsigned int version)
{
ar & stops ;//will automatically precede the array's length
} public
:
Bus_route () {}
};
6. Serialization of STL Containers
#include <boost/serialization/list.hpp>
class Bus_route
{
friend class Boost::serialization:: Access;
Std::list<bus_stop> stops;
Template<class archive>
void serialize (Archive & AR, const unsigned int version)
{
ar & stops;//will automatically add the container's length
} public
:
Bus_route () {}
};
7. Can be serialized according to version number
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/version.hpp>//must include this header file
class Bus_route
{
friend class Boost:: serialization::access;
Std::list<bus_stop> stops;
std::string driver_name;
Template<class archive>
void serialize (Archive & AR, const unsigned int version)
{
//Only save/ The load driver_name for newer archives
if (Version > 0)//is serialized
AR & driver_name According to the version number;
Ar & stops;
}
Public:
Bus_route () {}
};
Boost_class_version (Bus_route, 1)//Definition Version=1
8. Read and write separation
It's always been serialize. This function is serializable, but sometimes the methods of serialization and deserialization are inconsistent, and the Serialize function is difficult to support, so use the Save/load function, save to do the serialization, and the load to do the deserialization operation.
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/split_member.hpp>
class Bus_ Route
{
friend class boost::serialization::access;
Std::list<bus_stop> stops;
std::string driver_name;
Template<class archive>
void Save (Archive & AR, const unsigned int version) const
{
//note, vers Ion is always the latest when saving
ar & driver_name;
Ar & stops;
}
Template<class archive>
void Load (Archive & AR, const unsigned int version)
{
if (version > 0 )
ar & driver_name;
Ar & stops;
}
Boost_serialization_split_member ()//The macro definition must add public
:
Bus_route () {}} to the class
;
Boost_class_version (Bus_route, 1)