Is it possible to Serialize and Deserialize objects in C ++?

As we know, C ++ is also an object-oriented programming language, where most objects are Java. Therefore, I would like to know that the functions Serialize and deserializ are available in C ++, and also do we do this in java?

If so, how can this be achieved?

In java, we use the Serializable interface to say that this type of object can be serialized and deserialized.

So in C ++ how?

And out of curiosity, is this the same in C # as it is in java?

+4
source share
8 answers

In C ++ there is no built-in function. You will have to use external libraries like Boost .

+2
source

You can use the stack storage without using the "new" operator, so the class is essentially packaged as a structure and then exports the entire memory area to a file. When you want again, select the memory area and read the class data back into it from the file, and then access it as usual. It will not be portable, but it will work. It's pretty good on machines like Nintendo DS to store level data when you save something using an editor, although of course it’s not very (and dangerous for complex systems, besides!)

Note. I do not recommend that you do this, but it is valid on some embedded platforms, as already mentioned. I just wanted to post something interesting that homegrown developers actually do in C ++ on Nintendo DS when developing using palib.

+1
source

Java and C # support reflection . Basically, they can find enough information from an object / class for meaningful automatic serialization / deserialization.

C ++ language does not have reflection , for example, you cannot iterate over the fields of a class. Therefore, you should use a more or less manual method. Of course, there are libraries like Boost :: serialization to help.

+1
source

Even in java, the Serializable interface is just one approach. I would rather look at protocol buffers here; there are implementations of java, C ++ and C # (/.NET), and many others ; providing you with interoperability / portability in addition to fast, efficient binary serialization.

+1
source

usually you can write a function called serialize () and then have something like this:

 ofstream outFile; outFile.open (dirFileString.c_str(), ios::binary); outFile.write (reinterpret_cast < char *>(&x), sizeof (x)); outFile.write (reinterpret_cast < char *>(&y), sizeof (y)); 

and then perform a similar function:

 inFile.read (reinterpret_cast < char *>(&x), sizeof (x)); inFile.read (reinterpret_cast < char *>(&y), sizeof (y)); 

You can do such things for as many variables as possible inside the class object. Greetings.

+1
source

There are various libraries to support serialization for C ++, but this is a bit more complicated work. Serialization in Java depends on the fact that objects form a single monolithic tree, which is completely different in C ++.

0
source

Use Qt4 http://qt.nokia.com/doc/qdatastream.html to provide your overrides for the <operator and the β†’ operator

 template <class KT, class VT> inline QDataStream &operator>>(QDataStream &in, SOMap<KT, VT> &map) { QDataStream::Status oldStatus = in.status(); in.resetStatus(); map.clear(); quint32 n; in >> n; for (quint32 i = 0; i < n; ++i) { if (in.status() != QDataStream::Ok) break; KT key; VT value; in >> key >> value; map.append(key, value); } if (in.status() != QDataStream::Ok) map.clear(); if (oldStatus != QDataStream::Ok) in.setStatus(oldStatus); return in; } template <class KT, class VT> inline QDataStream &operator<<(QDataStream &out, const SOMap<KT, VT> &map) { out << quint32(map.size()); for(int i = 0, c = map.size(); i < c; ++i) { typename SOMap<KT, VT>::pair_type n = map.at(i); out << n.first << n.second; } return out; } 
0
source

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


All Articles