Serialization and class versioning
Hi, I have an application that uses serialization for data storage/retrieval, based on Boost 1.67.0. I've implemented class versioning as per: https://www.boost.org/doc/libs/1_67_0/libs/serialization/doc/tutorial.html#v ersioning The overall archive stores a number of sub-classes, nested within the XML. However, I get a run-time exception thrown that I can't catch when one of my sub-classes (DisplayDefinition) returns from being read in. A bit of debugging found that the problem was caused by the class version number having a value of 0, whilst the file being read had a version number of 3 for the class in question. Here's the bit of the class responsible for the archiving: class DisplayDefinition: public MessageDefinition { ... private: friend std::ostream & operator<<(std::ostream &os, const DisplayDefinition &DD); friend std::istream & operator>>(std::istream &is, const DisplayDefinition &DD); friend class boost::serialization::access; template<class Archive> void serialize(Archive & myArchive, const unsigned int version) { try { const char * cLabelTag = "Label"; myArchive & boost::serialization::make_nvp( cLabelTag, wstrLabel); const char * cAddressBytesTag = "SysExAddressBytes"; myArchive & boost::serialization::make_nvp( cAddressBytesTag, vSysExAddressBytes); const char * cActionsTag = "Actions"; myArchive & boost::serialization::make_nvp( cActionsTag, myActions); const char * cStatesTag = "StateNames"; myArchive & boost::serialization::make_nvp( cStatesTag, StateLabels); const char * cLineCountTag = "LineCount"; myArchive & boost::serialization::make_nvp( cLineCountTag, nDisplayLineCount); const char * cLineLengthTag = "LineLength"; myArchive & boost::serialization::make_nvp( cLineLengthTag, nDisplayLength); const char * cStripCountTag = "StripCount"; myArchive & boost::serialization::make_nvp( cStripCountTag, nStripCount); const char * cIsLEDLampTag = "LEDLamp"; myArchive & boost::serialization::make_nvp( cIsLEDLampTag, blnIsLEDLamp); const char * cTranslationTag = "TranslationID"; myArchive & boost::serialization::make_nvp( cTranslationTag, strTranslationID); const char * cCursorOffsetTag = "CursorOffset"; myArchive & boost::serialization::make_nvp( cCursorOffsetTag, nCursorOffset); if(version > 0) { const char * cCursorFromLeftTag = "CursorFromLeft"; myArchive & boost::serialization::make_nvp( cCursorFromLeftTag, blnCursorFromLeft); } if(version > 1) { const char * cUniCodeTag = "Unicode"; myArchive & boost::serialization::make_nvp( cUniCodeTag, blnIsUniCode); } if(version > 2) { const char * cLinkedHashTag = "LinkedTo"; myArchive & boost::serialization::make_nvp( cLinkedHashTag, strLinkedDisplayHash); } } // end try catch (...) { throw ID_LOAD_DATA_EXCEPTION; } // end catch } ... }; // end of class definition BOOST_CLASS_VERSION(DisplayDefinition, 3); I'm clueless as to why the BOOST_CLASS_VERSION macro doesn't seem to be setting the version number correctly. Can anybody advise me, please? Best wishes. Tim Burgess Raised Bar Ltd. E: mailto:tim@raisedbar.net tim@raisedbar.net M: +44 (0)7989 486976
On 8/2/18 4:58 AM, Tim Burgess via Boost-users wrote:
Hi,
I have an application that uses serialization for data storage/retrieval, based on Boost 1.67.0. I’ve implemented class versioning as per:
https://www.boost.org/doc/libs/1_67_0/libs/serialization/doc/tutorial.html#v...
The overall archive stores a number of sub-classes, nested within the XML. However, I get a run-time exception thrown that I can’t catch when one of my sub-classes (DisplayDefinition) returns from being read in. A bit of debugging found that the problem was caused by the class version number having a value of 0, whilst the file being read had a version number of 3 for the class in question.
A couple of possibilites a) something else is messed up and the values get out of sync. Using XML should be detected as it checks the end tags to be sure they match the start tags. So this is not likely it. b) determine how the input file got the version number 3 in the first place. Did it previously have a version # of 3? Did you change it back to 0 - lowering the version # would create this problem. c) If it's showing up on newly create files - it's easy, just debug and find the place where the version # is being written. Robert Ramey
Hi Robert,
Many thanks for coming back to me. I'm afraid I gave you bad information -
the version number in the file is 0, whilst the class definition is version
3, so the file should be read in and the additional class elements would be
added at run-time and could be saved back to a new edition of the file. I'm
confused because I have other classes using the same technique that don't
blow up. No exceptions are thrown until the serialization routine is
complete and an attempt is made to assign the result to a variable. I've
written a test harness that successfully creates and reads new files - the
problem lies in importing older files.
Best wishes.
Tim Burgess
-----Original Message-----
From: Boost-users
Hi,
I have an application that uses serialization for data storage/retrieval, based on Boost 1.67.0. I've implemented class versioning as per:
https://www.boost.org/doc/libs/1_67_0/libs/serialization/doc/tutorial. html#versioning
The overall archive stores a number of sub-classes, nested within the XML. However, I get a run-time exception thrown that I can't catch when one of my sub-classes (DisplayDefinition) returns from being read in. A bit of debugging found that the problem was caused by the class version number having a value of 0, whilst the file being read had a version number of 3 for the class in question.
A couple of possibilites a) something else is messed up and the values get out of sync. Using XML should be detected as it checks the end tags to be sure they match the start tags. So this is not likely it. b) determine how the input file got the version number 3 in the first place. Did it previously have a version # of 3? Did you change it back to 0 - lowering the version # would create this problem. c) If it's showing up on newly create files - it's easy, just debug and find the place where the version # is being written. Robert Ramey _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
OK,
A closer inspection of the XML whilst more awake shows that my old files
have a header in the form:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
On Behalf Of Tim
Burgess via Boost-users
Sent: 02 August 2018 21:29
To: boost-users@lists.boost.org
Cc: tim@raisedbar.net
Subject: Re: [Boost-users] Serialization and class versioning
Hi Robert,
Many thanks for coming back to me. I'm afraid I gave you bad information -
the version number in the file is 0, whilst the class definition is version
3, so the file should be read in and the additional class elements would be
added at run-time and could be saved back to a new edition of the file. I'm
confused because I have other classes using the same technique that don't
blow up. No exceptions are thrown until the serialization routine is
complete and an attempt is made to assign the result to a variable. I've
written a test harness that successfully creates and reads new files - the
problem lies in importing older files.
Best wishes.
Tim Burgess
-----Original Message-----
From: Boost-users
Hi,
I have an application that uses serialization for data storage/retrieval, based on Boost 1.67.0. I've implemented class versioning as per:
https://www.boost.org/doc/libs/1_67_0/libs/serialization/doc/tutorial. html#versioning
The overall archive stores a number of sub-classes, nested within the XML. However, I get a run-time exception thrown that I can't catch when one of my sub-classes (DisplayDefinition) returns from being read in. A bit of debugging found that the problem was caused by the class version number having a value of 0, whilst the file being read had a version number of 3 for the class in question.
A couple of possibilites a) something else is messed up and the values get out of sync. Using XML should be detected as it checks the end tags to be sure they match the start tags. So this is not likely it. b) determine how the input file got the version number 3 in the first place. Did it previously have a version # of 3? Did you change it back to 0 - lowering the version # would create this problem. c) If it's showing up on newly create files - it's easy, just debug and find the place where the version # is being written. Robert Ramey _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Robert Ramey
-
tim@raisedbar.net