I am trying to deserialize my Command object using the boost library. My goal is obviously to get the serialized object and pass it through the deserializer.
My class:
class Command {
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& arch, const unsigned int version)
{
arch& letter;
arch& x;
arch& y;
arch& button;
}
char letter;
int x;
int y;
std::string button;
public:
Command(char _letter, int _x, int _y, std::string _button) {
letter = _letter;
x = _x;
y = _y;
button = _button;
}
Command() {}
~Command() {}
char getLetter() { return letter; }
int getX() { return x; }
int getY() { return y; }
std::string getButton() { return button; }
void printCommand() {
std::cout << "letter: " << letter << std::endl;
std::cout << "x : " << x << std::endl;
std::cout << "y : " << y << std::endl;
std::cout << "button: " << button << std::endl;
std::cout << "================" << std::endl;
}
};
I used the boost library to serialize the object using this code:
template <class T>
std::string serialize(T obj) {
std::ofstream ofs("output");
{
boost::archive::text_oarchive oa(ofs);
oa << obj;
}
return "output";
}
Since I am sending those serialized objects through a TCP server, I need to serialize them but the deserialization code I use doesn't work:
template <class T>
T deSerialize(std::string s) {
T t = T();
std::ifstream ifs(s);
boost::archive::text_iarchive ia(ifs);
ia >> t;
return t;
}
I think my understanding of the process is lacking so I can't seem to understand why it doesn't work.