Preferred way to serialize an object using C ++

Java has a very easy way to serialize objects. could not find something like this in C ++. I found Boost Serialization and a basic approach using ifstream and ofstream.

I have a Task class that has a title, id, status and date / time. I want to implement a serialization function inside a class in order to store this object in binary mode. But I want to save multiple instances of the class in a single binary. Say an array of tasks.

Would there be a good approach to implementing a serialization method in a class? It will only serialize one at a time, and it looks bad if I use ifstream / ofstream, since I will open and close files a lot. Also, each task will be saved in different files.

Serialization acceleration looked great, but it's best to avoid third-party dependencies if possible.

What would be the best way to accomplish this?

My class header:

#ifndef TASK_H #define TASK_H class Task { public: enum Status { COMPLETED, PENDIENT }; Task(std::string text); ~Task(); // SETTERS void setText(std::string text); void setStatus(Status status); // GETTERS const std::string getText() const; const bool getStatus() const; const int getID() const; const int getCount() const; const std::string getDate() const; const std::string getTime() const; // DATE const int getDay() const; const int getMonth() const; const int getYear() const; // TIME const int getSecond() const; const int getMinute() const; const int getHour() const; // UTILS //serialize const void printFormatted() const; // OVERLOAD bool operator==( const Task &text2 ) const; private: void setID(); static int sCount; int id; std::string text; Status status; tm *timestamp; }; #endif 
+4
source share
3 answers

If you have only one, very simple class for serialization, it is not so difficult to implement a serialization function that records several members that you need to save. You do not give an example of the code that your class shows, but with several members, it should be relatively simple if there are no pointers. If you write out the number of serialized objects, followed by the data contained in them, this will probably be enough for your purposes.

If you want to implement serialization yourself, I will have an external function (perhaps familiar to your class) that handles the serialization of the Task array, instead of putting the serialization of the array in your class. What you can do is add the serialiseObject() function to your class, which serializes a single object, and then call it again from the array serialization function. This is a much cleaner design than serializing an array, also attached to the class itself.

Once you get to the C ++ serialized object, which is a little more complicated, especially with references and pointers, serialization very quickly becomes a difficult problem, and you really want to use the existing third-party mechanism, which was good testing.

However, as someone who is developing C ++ for life, I consider the addiction dependency to be a normal and not a third-party library that I would like to avoid. Boost gives you so many extra features that I consider part of "my standard library."

+5
source

Since there is no standard for serializing data in C ++, the โ€œpreferredโ€ method is preferred. Boost is fully acceptable if only your project explicitly prohibits the use of third-party libraries, in which case you can certainly use your own.

If you decide to tip over, make sure that your serializers and deserializers do not open or close threads on their own. Instead, callers should pass the stream to them. See this link for more details.

+2
source

I also used google protobuf for serialization. Although Google protobuf is not strictly serializable, it can be a very efficient and cross platform for data containers.

0
source

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


All Articles