Reprinted from: http://hi.baidu.com/ewook/item/0260f7f3e9082910d6ff8c7f
Serialization refers to the process of converting object state information to storage or transmission. During serialization, the object writes the current state to a temporary or persistent storage area. Later, you can re-create the object by reading or deserializing the object status from the bucket.
Object serialization deserialization is usually used:
1. Store the object on the hard disk
2. Transmit the object's byte sequence on the network
More Introduction
Common C ++ serialization Solutions
= Boost. serialization =
Introduction: Boost. serialization allows you to create or recreate equivalent structures in a program and save them as binary data, text data, XML, or other user-defined files. This library has the following attractive features:
1. Code portability (implementation only depends on ansi c ++)
2. Deep pointer saving and Restoration
3. STL containers and other common template libraries can be serialized.
4. Data portability
5. Non-Intrusive
Use: Tutorial
Download the boost library and compile it as needed (the boost library is widely used ).
Write Applications.
● Write intrusive or non-intrusive serialization methods as needed
● Support serialization of STL containers, pointers, and parent and child classes
Add serialization code for the class to be serialized.
● Initialize fstream
● Require initialization of XML, text, and binary Archives
● Write the object and close the file stream
= MFC serialization =
Introduction: The serialization method in MFC can be used in windows. MFC provides built-in support for serialization in the cobject class. Therefore, all classes derived from cobject can use the cobject serialization protocol. (Description in msdn)
Use:
Add MFC support for vs Projects
● Set Project Properties
● Contains the header file C ++:
1 # include <afxwin. h>
2 # include <afxtempl. h>
● Compile a class that inherits cobject
● Serialization method C ++:
1 void serialize (carchive & AR );
● Add serialization macro C ++:
1 // Add it to the Declaration class
2 declare_serial (basic_pojo_mfc)
3 //...
4 // Add it to the implementation file
5 implement_serial (myobject, cobject, 1)
● Compile serialized and deserialized objects
● Create cfile and carchive objects
● Write objects and close Resources
= Google protocol buffers =
Introduction: Google protocol buffers (GPB) is a data encoding method used inside Google to replace XML for data exchange. It can be used for data serialization and deserialization. Main features include:
1. Efficient
2. language neutral (CPP, Java, Python)
3. scalability
Official documentation
Use:
● Download GPB and compile the library to be used.
● Compile the. proto file and compile the. CC and. H files.
● Write. proto according to rules
● Compile
● Shell/CMD:
1 protoc-I = $ src_dir-cpp_out = $ dst_dir $ src_dir/addressbook. proto
● Compile the serialization and deserialization code.
Comparison
= Test case introduction =
Comparison dimension:
● Time consumed by serialization and deserialization
● Data file size generated
Test Data Type:
C ++: 01 // Basic Data Type
02 class basic_pojo {
03 public:
04 char char8;
05 unsigned char uchar8;
06 short limit 16;
07 unsigned short ushort16;
08 int int32;
09 unsigned int uint32;
10 long long32;
11 unsigned long ulong32;
12 float float32;
13 double double64;
14 bool bool8;
15 };
16
17 // Composite data type
18 class complex_pojo {
19 public:
20 string string_stl;
21 basic_pojo_boost basic_class;
22 };
Test code
Result
Time consumed by serialization and deserialization:
Data File Size:
Data:
Protocol buffersboost. serializationmfc: serializationserialization (MS) 942191218 unserialization (MS) 2032961282 archive size (Kb) 459044935372
Conclusion
Google protocol buffers is highly efficient, but data objects must be pre-defined and compiled using protoc. It is suitable for internal use that requires efficiency and allows custom types. Boost. serialization is flexible and simple to use, and supports Standard C ++ containers. Compared with MFC, MFC is less efficient, but it is most convenient to use it with the msvs platform. We hope to have time to add more serialization solutions and test indicators.