无法反序列化 std::list<std::string>

Cannot deserialize std::list<std::string>

本文关键字:std gt string lt list 反序列化      更新时间:2023-10-16
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
class SerializableSmth 
{
    friend class boost::serialization::access;
    private:
        std::list<std::string> data;
        template<class Archive> void serialize(Archive & ar, unsigned int version)
        {
            ar & data;
        }
};
BOOST_CLASS_VERSION(SerializableSmth, 1);

首先看一下这个问题:当data包含超过~7个字符的字符串时,它不能被序列化回。序列化的text_oarchive看起来像这样:

22 serialiation::archive 10 0 1 0 0 2 0 5 test1 13 test2-914166-

(当test2-被缩短为5 test2-时,它工作正常)。


当使用text_oarchivestd::stringstream进行序列化并使用boost::iostreams::basic_array进行反序列化时,会发生这种情况。

这个问题似乎正在通过设置在输出和输入存档上的boost::archive::no_header | boost::archive::no_codecvt标志得到修复。

我无法复制它,但也许这个SSCCE可以帮助您发现问题:

查看Live On Coliru

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <sstream>
class SerializableSmth 
{
    public:
    void init() {
        for  (int i=0; i<100; ++i) {
            data.push_back("Some fairly long string " + std::to_string(i) + " with a number");
        }
    }
    friend class boost::serialization::access;
    std::list<std::string> data;
    template<class Archive> void serialize(Archive & ar, unsigned int version)
    {
        ar & data;
    }
};
BOOST_CLASS_VERSION(SerializableSmth, 1);
#include <iostream>
int main()
{
    std::string serialized;
    {
        std::stringstream ss;
        boost::archive::text_oarchive oa(ss);
        SerializableSmth original;
        original.init();
        oa << original;
        serialized = ss.str();
    }
    {
        boost::iostreams::basic_array_source<char> as(serialized.data(), serialized.size());
        boost::iostreams::stream<boost::iostreams::basic_array_source<char> > is(as);
        // now let's see it back:
        boost::archive::text_iarchive ia(is);
        SerializableSmth cloned;
        ia >> cloned;
        std::cout << "Cloned data has " << cloned.data.size() << " recordsn";
    }
}

打印

Cloned data has 100 records
像预期的那样

序列化的数据为一行,约4.5kB:

22 serialization::archive 10 0 1 0 0 100 0 39 Some fairly long string 0 with a number 39 Some fairly long string 1 with a number 39 Some fairly long string 2 with a number 39 Some fairly long string 3 with a number 39 Some fairly long string 4 with a number 39 Some fairly long string 5 with a number 39 Some fairly long string 6 with a number 39 Some fairly long string 7 with a number 39 Some fairly long string 8 with a number 39 Some fairly long string 9 with a number 40 Some fairly long string 10 with a number 40 Some fairly long string 11 with a number 40 Some fairly long string 12 with a number 40 Some fairly long string 13 with a number 40 Some fairly long string 14 with a number 40 Some fairly long string 15 with a number 40 Some fairly long string 16 with a number 40 Some fairly long string 17 with a number 40 Some fairly long string 18 with a number 40 Some fairly long string 19 with a number 40 Some fairly long string 20 with a number 40 Some fairly long string 21 with a number 40 Some fairly long string 22 with a number 40 Some fairly long string 23 with a number 40 Some fairly long string 24 with a number 40 Some fairly long string 25 with a number 40 Some fairly long string 26 with a number 40 Some fairly long string 27 with a number 40 Some fairly long string 28 with a number 40 Some fairly long string 29 with a number 40 Some fairly long string 30 with a number 40 Some fairly long string 31 with a number 40 Some fairly long string 32 with a number 40 Some fairly long string 33 with a number 40 Some fairly long string 34 with a number 40 Some fairly long string 35 with a number 40 Some fairly long string 36 with a number 40 Some fairly long string 37 with a number 40 Some fairly long string 38 with a number 40 Some fairly long string 39 with a number 40 Some fairly long string 40 with a number 40 Some fairly long string 41 with a number 40 Some fairly long string 42 with a number 40 Some fairly long string 43 with a number 40 Some fairly long string 44 with a number 40 Some fairly long string 45 with a number 40 Some fairly long string 46 with a number 40 Some fairly long string 47 with a number 40 Some fairly long string 48 with a number 40 Some fairly long string 49 with a number 40 Some fairly long string 50 with a number 40 Some fairly long string 51 with a number 40 Some fairly long string 52 with a number 40 Some fairly long string 53 with a number 40 Some fairly long string 54 with a number 40 Some fairly long string 55 with a number 40 Some fairly long string 56 with a number 40 Some fairly long string 57 with a number 40 Some fairly long string 58 with a number 40 Some fairly long string 59 with a number 40 Some fairly long string 60 with a number 40 Some fairly long string 61 with a number 40 Some fairly long string 62 with a number 40 Some fairly long string 63 with a number 40 Some fairly long string 64 with a number 40 Some fairly long string 65 with a number 40 Some fairly long string 66 with a number 40 Some fairly long string 67 with a number 40 Some fairly long string 68 with a number 40 Some fairly long string 69 with a number 40 Some fairly long string 70 with a number 40 Some fairly long string 71 with a number 40 Some fairly long string 72 with a number 40 Some fairly long string 73 with a number 40 Some fairly long string 74 with a number 40 Some fairly long string 75 with a number 40 Some fairly long string 76 with a number 40 Some fairly long string 77 with a number 40 Some fairly long string 78 with a number 40 Some fairly long string 79 with a number 40 Some fairly long string 80 with a number 40 Some fairly long string 81 with a number 40 Some fairly long string 82 with a number 40 Some fairly long string 83 with a number 40 Some fairly long string 84 with a number 40 Some fairly long string 85 with a number 40 Some fairly long string 86 with a number 40 Some fairly long string 87 with a number 40 Some fairly long string 88 with a number 40 Some fairly long string 89 with a number 40 Some fairly long string 90 with a number 40 Some fairly long string 91 with a number 40 Some fairly long string 92 with a number 40 Some fairly long string 93 with a number 40 Some fairly long string 94 with a number 40 Some fairly long string 95 with a number 40 Some fairly long string 96 with a number 40 Some fairly long string 97 with a number 40 Some fairly long string 98 with a number 40 Some fairly long string 99 with a number