How does versioning work when using Boost Serialization for Derived Classes?
- by Venkata Adusumilli
When a Client serializes the following data:
InternationalStudent student;
student.id("Client ID");
student.firstName("Client First Name");
student.country("Client Country");
the Server receives the following:
ID = "Client ID"
Country = "Client First Name"
instead of the following:
ID = "Client ID"
Country = "Client Country"
The only difference between the Server and Client classes is the First Name of the Student. How can we make the Server ignore First Name recieved from the Client and process the Country?
Server Side Classes
class Student
{
public:
Student(){}
virtual ~Student(){}
public:
std::string id() { return idM; }
void id(std::string id) { idM = id; }
protected:
friend class boost::serialization::access;
protected:
std::string idM;
protected:
template<class A>
void serialize(A& archive, const unsigned int /*version*/)
{
archive & BOOST_SERIALIZATION_NVP(idM);
}
};
class InternationalStudent : public Student
{
public:
InternationalStudent() {}
~InternationalStudent() {}
public:
std::string country() { return countryM; }
void country(std::string country) { countryM = country; }
protected:
friend class boost::serialization::access;
protected:
std::string countryM;
protected:
template<class A>
void serialize(A& archive, const unsigned int /*version*/)
{
archive & BOOST_SERIALIZATION_NVP(boost::serialization::base_object<Student>(*this));
archive & BOOST_SERIALIZATION_NVP(countryM);
}
};
Client Side Classes
class Student
{
public:
Student(){}
virtual ~Student(){}
public:
std::string id() { return idM; }
void id(std::string id) { idM = id; }
std::string firstName() { return firstNameM; }
void firstName(std::string name) { firstNameM = name; }
protected:
friend class boost::serialization::access;
protected:
std::string idM;
std::string firstNameM;
protected:
template<class A>
void serialize(A& archive, const unsigned int /*version*/)
{
archive & BOOST_SERIALIZATION_NVP(idM);
if (version >=1)
{
archive & BOOST_SERIALIZATION_NVP(firstNameM);
}
}
};
BOOST_CLASS_VERSION(Student, 1)
class InternationalStudent : public Student
{
public:
InternationalStudent() {}
~InternationalStudent() {}
public:
std::string country() { return countryM; }
void country(std::string country) { countryM = country; }
protected:
friend class boost::serialization::access;
protected:
std::string countryM;
protected:
template<class A>
void serialize(A& archive, const unsigned int /*version*/)
{
archive & BOOST_SERIALIZATION_NVP(boost::serialization::base_object<Student>(*this));
archive & BOOST_SERIALIZATION_NVP(countryM);
}
};