无法反序列化 std::list<std::string>
Posted
技术标签:
【中文标题】无法反序列化 std::list<std::string>【英文标题】:Cannot deserialize std::list<std::string> 【发布时间】:2014-05-26 15:21:10 【问题描述】:#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
进行反序列化时会发生这种情况。
【问题讨论】:
boost::iostreams::basic_array?怎么样? 【参考方案1】:在输出和输入档案上设置boost::archive::no_header | boost::archive::no_codecvt
标志似乎解决了这个问题。
【讨论】:
+1 有趣的东西。我其实不知道这些存档标志【参考方案2】:我无法重现它,但也许这个 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() << " 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
【讨论】:
以上是关于无法反序列化 std::list<std::string>的主要内容,如果未能解决你的问题,请参考以下文章
为啥 std::move 不能与 std::list 一起使用