Hi, I'm currently trying to serialize and deserialize a derived class using boost::archive::xml_iarchive/xml_oarchive Version 1.52. Everything works fine as long as I serialize and deserialize the same class (Example 1). The attributes of the derived class and the base class are both serialized and the deserialized correctly and requestPackageOut equals requestPackageIn (the base-class attributes are equal too). But if I serialize the derived class "RequestPackage" and try to deserialize it into the base-class "Package" (Example 2) I'm getting an "input stream error" exception while trying to deserialize with ia >> BOOST_SERIALIZATION_NVP(*package);. I'm quite new to this topic and found a lot of posting but none of them seems to match and solve my exact problem. It would by very nice if somebody could help me with that. Background information: I want to use xml-serialized objects for data exchange via network and want all the packages to inherit from Package-class which carries "packageId" and therefore give me the chance to identify the incoming xml-objects and handle every package based on this id. If some information is missing I will do my best to provide it later. Torsten Example 1: int main(int argc, char** argv){ std::cout << "Started!" << std::endl; std::string serializedObject; RequestPackage requestPackageOut; RequestPackage requestPackageIn; requestPackageOut.setPackageId(3); requestPackageIn.setPackageId(0); Serializer::serialize(&serializedObject,&requestPackageOut); Serializer::deserialize(&serializedObject,&requestPackageIn); return 0; } Example 2: int main(int argc, char** argv){ std::cout << "Started!" << std::endl; std::string serializedObject; Package package; RequestPackage requestPackageOut; RequestPackage requestPackageIn; requestPackageOut.setPackageId(3); requestPackageIn.setPackageId(0); Serializer::serialize(&serializedObject,&requestPackageOut); Serializer::deserialize(&serializedObject,&package); return 0; } The Rest: namespace Serializer{ template <typename T> int serialize(std::string *serializedObject, T *package){ int returnValue = 0; std::ostringstream objectOss; boost::archive::xml_oarchive oa(objectOss); try { oa << BOOST_SERIALIZATION_NVP(package); } catch (boost::archive::archive_exception const& ex) { returnValue = 1; } serializedObject->assign(objectOss.str()); return returnValue; }; template <typename T> int deserialize(std::string *serializedObject, T *package){ int returnValue = 0; std::istringstream objectIss(*serializedObject); boost::archive::xml_iarchive ia(objectIss); try { ia >> BOOST_SERIALIZATION_NVP(*package); } catch (boost::archive::archive_exception const& ex) { std::cerr << "Error: " << ex.what() << std::endl; std::cerr.flush(); returnValue = 1; } return returnValue; }; } class RequestPackage : public Package{ private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Package); ar & BOOST_SERIALIZATION_NVP(requestId); } int requestId; public: int getRequestId() const; void setRequestId(int requestId); }; class Package{ private: friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(packageID); } int packageID; public: int getPackageId() const; void setPackageId(int packageId); };
Torsten Wylegala wrote
But if I serialize the derived class "RequestPackage" and try to deserialize it into the base-class "Package" (Example 2) I'm getting an "input stream error" exception while trying to deserialize with ia >> BOOST_SERIALIZATION_NVP(*package);.
You can't do this. the class loaded must be the exact class saved.
Background information: I want to use xml-serialized objects for data exchange via network and want all the packages to inherit from Package-class which carries "packageId" and therefore give me the chance to identify the incoming xml-objects and handle every package based on this id.
a very common scenario. Look more carefully in the documentation. Look a tests - e.g. test_base_class. Also there are examples. -- View this message in context: http://boost.2283326.n4.nabble.com/Serialization-Deserialization-of-base-cla... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (2)
-
Robert Ramey
-
Torsten Wylegala