Serializing C ++ Objects

I would like to implement the Serialization class, which takes an object and converts it to a binary stream and stored in a file. Later, the object must be restored from the file.

Although this feature is provided by BinaryFormatter in C #, I would like to create my own serialization class from scratch.

Can someone point out some resources?

Thank you in advance

+4
source share
4 answers

I used the boost :: serialization library for a while, and I think it is very good. You just need to create the serialization code as follows:

class X { private: std::string value_; public: template void serialize(Archive &ar, const unsigned int version) { ar & value_; }; } 

No need to create de-serialization code (why did they use operator and operator). But if you prefer, you can still use the <and → operators.

It is also possible to write a serialization method for a class without changes (for example: if you need to serialize an object that comes from the library). In this case, you should do something like:

 namespace boost { namespace serialization { template void serialize(Archive &ar, X &x const unsigned int version) { ar & x.getValue(); }; }} 
+2
source

I would like to give you a negative answer. It is less useful, but it can still be.

I have been using boost serialization for several years, and this has been one of my company's main strategic mistakes. It produces a very large output, it is very slow, it distributes a whole bunch of dependencies, which makes everything impossible with slow compilation, and then it is difficult to exit, because you have existing serialized formats. In addition, it behaves differently on different compilers, so the upgrade from VS2005 to 2010 actually forced us to write a compatibility level, which is also difficult because the code is very difficult to understand.

+5
source

Here are 2 solutions for C ++ serialization:

I personally only have experience with 1st, and in fact I only used text-based serializers, but I know that it is easy to define binary serializers for use with s11n.

+3
source

C ++ Middleware Writer may be of interest. It has performance advantages over the Boost serialization library . It also automates the creation of serialization functions.

0
source

Source: https://habr.com/ru/post/1435103/


All Articles