Java has a very simple way to serialize objects. couldn't find something similar in C++. I found Boost Serialization, and a basic approach using ifstream and ofstream.
I have a Task class wich has a title, id, status and date/time. I want to implement a serialize function within the class to save that object in binary mode. But I want to save multiple instances of the class on a single binary file. Let's say, a Task array.
Would it be a good approach to implement a serialize method on the class? It will only serialize one at a time and It doesn't looks well-done if I use ifstream/ofstream, as I'll be opening and closing files a lot. Also every task will be saved on different files.
Boost's serialization looked fine, but is best for me to avoid 3rd party dependencies if possible.
What would be the best way of 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
For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object.
Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.
If you only have a single, very simple class to serialise out, it's not that hard to implement a serialisation function that writes out the handful of members that you need to save. You don't give a code example that shows your class, but with a handful of members it should be comparatively easy as long as there are no pointers involved. If you write out the number of objects serialised followed by the data contained therein, that would probably be good enough for your purposes.
If you want to implement the serialisation yourself, I would have an external function (possibly a friend of your class) handle the serialisation of the Task array rather than trying to put the array serialisation into your class. What you can do is add a serialiseObject()
function to your class that serialises a single object, then call it repeatedly from the array serialisation function. That's a much cleaner design than having the array serialisation also bolted onto the class itself.
Once you get into serialising C++ object that are a little more complex, especially ones containing references and pointers, serialisation very quickly becomes a hard problem and you really, really want to use an existing, third-party mechanism that has been well tested.
That said, as someone who does C++ development for a living, I consider a dependency on boost as normal, not a third party library I would want to avoid. Boost gives you so much additional functionality that I consider it part of "my standard library".
Since there is no standard for serializing data in C++, the "preferred" way is what you prefer. Boost is entirely acceptable, unless your project explicitly prohibits the use of third-party libraries, in which case you can certainly roll your own.
If you choose to roll your own, make sure that your serializers and deserializers do not open and close the streams on their own. Instead, the callers need to pass the stream to them. See this link for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With