Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the format of boost.serialization's output

I tried to serialize a vector and a map container and output their value by cout. However, it is hard for me to get the meaning of boost's output. My code looks like this:

#include <iostream>
#include <boost/serialization/vector.hpp>   
#include <boost/serialization/map.hpp>  
#include <boost/assign.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
    vector<int> v = boost::assign::list_of(1)(3)(5);
    map<int, string> m = boost::assign::map_list_of(1,"one")(2,"two");

    std::stringstream ss;
    boost::archive::text_oarchive oa(ss);
    oa<<v<<m;   

    vector<int> v_;
    map<int,string> m_;
    boost::archive::text_iarchive ia(ss);
    ia>>v_>>m_;
    boost::archive::text_oarchive ib(cout);
    ib<<v_<<m_;
    return 0;
}

The output looks like this:

22 serialization::archive 9 3 0 1 3 5 0 0 2 0 0 0 1 3 one 2 3 two

What's the meaning of the numbers 9 3 0 before the value 1 3 5 I compose? How about the 0 0 2 0 0 0 ? Does the '3' between '1' and 'one' mean there are 3 characters ?

like image 581
user957121 Avatar asked Apr 22 '13 11:04

user957121


2 Answers

I'm not sure about some zeros in the map (maybe some version number or tracking levels) but for the rest :

22 (length of the signature)
serialization::archive (signature)
9 (archive version, 10 on boost 1.53)
3 (vector size)
0 (item version)
1 3 5 (vector items)
0 (map class tracking level ?)
0 (map class version ?)
2 (map size)
0 (item class tracking _level ?)
0 (item class version ?)
0 (item version)
1 (key) 3 (value length) one (value)
2 (key) 3 (value length) two (value)

Note that the content and format of the text output is Boost's internal business and may change with future Boost revisions, so your application shouldn't rely on the internal archive contents.

like image 153
zakinster Avatar answered Oct 19 '22 11:10

zakinster


If you put these lines at the end of your code

boost::archive::xml_oarchive ib(cout);
ib<< boost::serialization::make_nvp("v", v_) << boost::serialization::make_nvp("m", m_);   //    ib<<v_<<m_;
return 0;

you will get this output, which describes it self:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">
<v>
    <count>3</count>
    <item_version>0</item_version>
    <item>1</item>
    <item>3</item>
    <item>5</item>
</v>
<m class_id="1" tracking_level="0" version="0">
    <count>2</count>
    <item_version>0</item_version>
    <item class_id="2" tracking_level="0" version="0">
        <first>1</first>
        <second>one</second>
    </item>
    <item>
        <first>2</first>
        <second>two</second>
    </item>
</m>
</boost_serialization>

So @zacinter is correct and the three 0 after the 2 are: 1) item_version (of std::pair, the value type of the map) 2) tracking_level of std::pair and 3) version of std::pair.

like image 6
alfC Avatar answered Oct 19 '22 09:10

alfC