2
#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);

A first look on the problem: when data contains strings longer than ~7 characters, it can't be serialized back. A serialized text_oarchive would look like this:

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

(when test2- is shortened to 5 test2-, it works ok).


This occurs when using text_oarchive over std::stringstream to serialize and boost::iostreams::basic_array to deserialize.

1
  • boost::iostreams::basic_array? how?
    – sehe
    May 26, 2014 at 15:51

2 Answers 2

1

The problem seems to be getting fixed by boost::archive::no_header | boost::archive::no_codecvt flags set on both output and input archives.

1
  • +1 for interesting stuff. I actually didn't know about these archive flags
    – sehe
    Jul 24, 2014 at 9:35
0

I cannot reproduce it, but maybe this SSCCE can help you spot the problem:

See it 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";
    }
}

Prints

Cloned data has 100 records

as expected

The serialized data is a single line of ~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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.