#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_oarchive
而不是std::stringstream
进行序列化并boost::iostreams::basic_array
进行反序列化时会发生这种情况。
答案 0 :(得分:1)
问题似乎是由输出和输入档案中设置的boost::archive::no_header | boost::archive::no_codecvt
标志修复。
答案 1 :(得分:0)
我无法重现它,但也许这个SSCCE可以帮助你发现问题:
#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() << " records\n";
}
}
打印
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