Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you serialize an object in C++?

I have a small hierarchy of objects that I need to serialize and transmit via a socket connection. I need to both serialize the object, then deserialize it based on what type it is. Is there an easy way to do this in C++ (as there is in Java)?

Are there any C++ serialization online code samples or tutorials?

EDIT: Just to be clear, I'm looking for methods on converting an object into an array of bytes, then back into an object. I can handle the socket transmission.

like image 813
Bill the Lizard Avatar asked Feb 07 '09 14:02

Bill the Lizard


People also ask

What is serialization in C?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is serialization with example?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

How do you serialize and deserialize an object?

The Serialization is a process of changing the state of an object into a byte stream, an object is said to be serializable if its class or parent classes implement either the Serializable or Externalizable interface and the Deserialization is a process of converting the serialized object back into a copy of an object.


1 Answers

Talking about serialization, the boost serialization API comes to my mind. As for transmitting the serialized data over the net, I'd either use Berkeley sockets or the asio library.

Edit:
If you want to serialize your objects to a byte array, you can use the boost serializer in the following way (taken from the tutorial site):

#include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp> class gps_position { private:     friend class boost::serialization::access;     template<class Archive>     void serialize(Archive & ar, const unsigned int version)     {         ar & degrees;         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)     {} }; 

Actual serialization is then pretty easy:

#include <fstream> std::ofstream ofs("filename.dat", std::ios::binary);      // create class instance     const gps_position g(35, 59, 24.567f);      // save data to archive     {         boost::archive::binary_oarchive oa(ofs);         // write class instance to archive         oa << g;         // archive and stream closed when destructors are called     } 

Deserialization works in an analogous manner.

There are also mechanisms which let you handle serialization of pointers (complex data structures like tress etc are no problem), derived classes and you can choose between binary and text serialization. Besides all STL containers are supported out of the box.

like image 54
newgre Avatar answered Oct 04 '22 16:10

newgre