在boost中寻找反序列化示例

时间:2014-04-11 21:26:42

标签: c++ visual-studio-2012 boost deserialization

我真的不确定如何开始进行提升反序列化。下面是想要知道boost反序列化代码如何的示例代码。

#include <boost/serialization/access.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/smart_ptr/make_shared.hpp>

namespace mydata
{
    struct MyInfo 
    {
        std::string info = "extra info";

        MyInfo(std::string info) : info(std::move(info)) {}

        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive &ar, const unsigned int /*version*/)
            {
                ar & info;
            }
    };

    struct MyData
    {
        std::string name;
        std::string type;
        boost::shared_ptr<MyInfo> myref;

        private:
        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive &ar, const unsigned int /*version*/)
            {
                ar & name;
                ar & type;
                ar & myref;
            }
    };
}



 int main()
    {

        using namespace mydata;
        MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") };

        boost::archive::text_oarchive oa(std::cout);
        oa << data;
    }

还有类似反序列化的链接,但不清楚  Boost deserialize a derived class to base class pointer

我真的不太了解..它真的很有帮助..如果存在的话,我还可以获得关于boost反序列化的链接。

1 个答案:

答案 0 :(得分:5)

你快到了那里:

int main()
{
    using namespace mydata;
    MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") };

    std::ostringstream oss;
    {
        boost::archive::text_oarchive oa(oss);
        oa << data;
    }

    std::istringstream iss(oss.str());
    {
        boost::archive::text_iarchive ia(iss);
        ia >> data;
    }
}

事实上,您可以使用std::stringstream作为输入和输出,但对于纯度,我展示了对称方法(执行冗余复制)。

你需要

#include <sstream>
#include <boost/archive/text_iarchive.hpp>

对于反序列化,您的类必须是默认构造的:

MyInfo(std::string info = "") : info(std::move(info)) {}

(不相关的警告: 在这里使用std::string info = {},因为这会在MSVC中触发编译器错误)

这是一个完整的工作示例,显示反序列化的对象具有相同的数据: Live On Coliru

#include <boost/serialization/access.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/smart_ptr/make_shared.hpp>

#include <sstream>
#include <boost/archive/text_iarchive.hpp>

namespace mydata
{
    struct MyInfo 
    {
        std::string info = "extra info";

        MyInfo(std::string info = "") : info(std::move(info)) {}

        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive &ar, const unsigned int /*version*/)
            {
                ar & info;
            }
    };

    struct MyData
    {
        std::string name;
        std::string type;
        boost::shared_ptr<MyInfo> myref;

        private:
        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive &ar, const unsigned int /*version*/)
            {
                ar & name;
                ar & type;
                ar & myref;
            }
    };
}

int main()
{
    using namespace mydata;

    std::ostringstream oss;
    {
        MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") };

        boost::archive::text_oarchive oa(oss);
        oa << data;
    }

    MyData cloned;
    std::istringstream iss(oss.str());
    {
        boost::archive::text_iarchive ia(iss);
        ia >> cloned;
    }

    // check equality
    {
        std::ostringstream oss2;

        boost::archive::text_oarchive oa(oss2);
        oa << cloned;

        std::cout << oss.str()  << "\n";
        std::cout << oss2.str() << "\n";
    }
}

输出:

22 serialization::archive 10 0 0 14 this is a name 14 this is a type 0 1 2 1 0
0 12 this is info

22 serialization::archive 10 0 0 14 this is a name 14 this is a type 0 1 2 1 0
0 12 this is info