Home > database >  Build error serializing struct to XML using Boost
Build error serializing struct to XML using Boost

Time:04-02

I'm trying to serialize some structs to XML using Boost. I can't change the structs, so I'm trying to do it non invasively.

Following the simple "non intrusive version" example in the docs I managed to get flat text serialization to work. However when I try to extend it to XML by looking at the XML example I'm unable to build and get errors from within the Boost libraries. I find only one hit on the error message and I'm unable to see how to apply it to my situation to solve it. Looking generally for other posts and examples, I only see the referenced ones that don't involve putting code inside the structs.

The code and error are below. Is anyone familiar enough with this to point out what I'm doing wrong?

The code is

#include <iostream>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <boost/config.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

void boostTest();

int main()
{
    boostTest();
}

struct s_FileInfo
{
    //const char* comment;
    int test;
    int motionCases; //number of motion cases
};

void boostTest()
{
    struct s_FileInfo test = { 3,2 };
    std::ofstream ofs("c:\\sandpit\\test.xml", std::ofstream::out); 
    boost::archive::xml_oarchive oa(ofs);
    oa << BOOST_SERIALIZATION_NVP(test);
    ofs.close();
}

namespace boost {
    namespace serialization {

        template<class Archive>
        void serialize(Archive& ar, s_FileInfo& g, const unsigned int version)
        {
            ar& g.test;
            ar& g.motionCases;
        }

    } // namespace serialization
} // namespace 

The errors I'm getting on attempting to build are

'mpl_assertion_in_line_6': const object must be initialized 'int boost::mpl::assertion_failed<false>(boost::mpl::assert<false>::type)': cannot convert argument 1 from 'boost::mpl::failed ************boost::serialization::is_wrapper<T>::* ***********' to 'boost::mpl::assert<false>::type'

Both are in boost\archive\basic_xml_oarchive.hpp

CodePudding user response:

The assert says it all:

// If your program fails to compile here, its most likely due to
// not specifying an nvp wrapper around the variable to
// be serialized.
BOOST_MPL_ASSERT((serialization::is_wrapper< T >));

So, let's add that

Live On Coliru

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <fstream>

void boostTest();
int main() { boostTest(); }

struct s_FileInfo {
    // const char* comment;
    int test;
    int motionCases; // number of motion cases
};

void boostTest() {
    s_FileInfo    test {3, 2};
    std::ofstream ofs("test.xml");
    {
        boost::archive::xml_oarchive oa(ofs);
        oa << BOOST_SERIALIZATION_NVP(test);
    }
}

namespace boost::serialization {
    template <class Ar> void serialize(Ar& ar, s_FileInfo& g, unsigned /*unused*/) {
        ar& BOOST_SERIALIZATION_NVP(g.test);
        ar& BOOST_SERIALIZATION_NVP(g.motionCases);
    }
} // namespace boost::serialization

Creates test.xml:

<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="19">
<test class_id="0" tracking_level="0" version="0">
    <g.test>3</g.test>
    <g.motionCases>2</g.motionCases>
</test>
</boost_serialization>
  • Related