【问题标题】:Serializing a map of objects to xml using boost::serialization使用 boost::serialization 将对象映射序列化为 xml
【发布时间】:2010-07-19 07:07:36
【问题描述】:

下面的序列化示例来自boost mailing list,这与我想做的几乎相同。但是,我更改了存档,以便将其序列化为 XML。如果我序列化为二进制文件,编译不会失败,但序列化为 xml 时会失败。 basic_xml_oarchive.hpp 编译失败,方法如下:

// boost code where compile fails
template<class T>
void save_override(T & t, BOOST_PFTO int)
{
    // 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>));
    this->detail_common_oarchive::save_override(t, 0);
}

似乎我没有做足够的工作来允许序列化std::map&lt;int, CSomeData&gt; 对象,有什么解决方法的想法吗?


我的序列化实现:

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

using namespace std;

// This is a test class to use as the map data.
class CSomeData {
    public:
        CSomeData(){};
        CSomeData(float f0, string str0)
        {
            m_f0 = f0;
            m_str0 = str0;
        }

        float m_f0;
        string m_str0;

    private:
        friend class boost::serialization::access;

        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & m_f0;
            ar & m_str0;
        }
};

// This is the class we really want to try serializing.
class CTest {
    public:
        CTest(){};
        CTest(int nNumber)
        {
            m_nNumber = nNumber;

            // Fill with some dummy data.
            m_mTst.insert(make_pair(0, CSomeData(0.23f, "hi hi hi")));
            m_mTst.insert(make_pair(1, CSomeData(7.65f, "second one")));
            m_mTst.insert(make_pair(2, CSomeData(9.23f, "third one")));
            m_mTst.insert(make_pair(3, CSomeData(5.6766, "chosen one")));
        }
        ~CTest(){};

        save()
        {
            std::ofstream ofs("filename");

            // Write class instance to archive. Writing seems to work ok.
            boost::archive::xml_oarchive oa(ofs);
            oa << BOOST_SERIALIZATION_NVP(*this);
        }

        int m_nNumber;

    private:
        map<int, CSomeData> m_mTst;

        friend class boost::serialization::access;

        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & m_nNumber;
            ar & m_mTst;
        }
};

【问题讨论】:

标签: c++ c++builder boost-serialization


【解决方案1】:

我相信您需要用 XML 序列化的名称标记成员。这指定要在 XML 中使用的元素名称。 IE。使用类似的东西:

ar & BOOST_SERIALIZATION_NVP(m_f0);

或(在这种情况下更好):

ar & make_nvp("field0", my_f0);

对于二进制序列化,标签将被忽略。更多细节在这里:

http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/wrappers.html

【讨论】:

  • 那不起作用,我已经在使用BOOST_SERIALIZATION_NVP
  • 仍然在链接时死了。我在上面发布的 codegear 链接说不支持序列化。无论如何感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2012-02-18
  • 2015-04-29
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多