Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::serialization - Serialization (non intrusive) of a map containing in and structure.
From: Brian Davis (bitminer_at_[hidden])
Date: 2014-03-11 13:00:28

Thanks Steven for the reply. While this was not the final answer to the
problem (as it was one problem) it did help lead to the solution which was
as Steven stated ..... and a missing nvp around the ni.a variable.

Note the std::pair<> code was not necessary and was removed.

below is working version and xml file for those who may experience this in
the future.


#include <string>
#include <fstream>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/version.hpp>

struct non_intrusive{
  int id;
  std::string name;

typedef std::map<int, non_intrusive> a_map_t;

namespace boost { namespace serialization {

template<class Archive>
void serialize( Archive& ar, non_intrusive& ni, const unsigned int version
    ar & boost::serialization::make_nvp( "id", );
    ar & boost::serialization::make_nvp( "name", );

// +----------------------------------------------------------------------

void save
 const a_map_t& map_var, const std::string& file_name
  boost::filesystem::path file_path = file_name;
  boost::filesystem::path pathstr = file_path.remove_leaf();
  if( pathstr != "" )
    boost::filesystem::create_directory( pathstr );

  std::ofstream ofs( file_name.c_str(), std::ios_base::out |

  boost::archive::xml_oarchive xml(ofs);
      xml << BOOST_SERIALIZATION_NVP( map_var );

// +----------------------------------------------------------------------

a_map_t load(const std::string& file_name)
  a_map_t map_var;
  // std::ifstream ifs(file_name.c_str(), std::fstream::binary |
  std::ifstream ifs(file_name.c_str() );
  if( !ifs )
      non_intrusive od; = -1; = "unknown device";

            a_map_t::value_type( 0, od ) );

      save( map_var, file_name );

      boost::archive::xml_iarchive xml(ifs);
      xml >> BOOST_SERIALIZATION_NVP( map_var );
  return map_var;

// +----------------------------------------------------------------------
int main( void ){

    a_map_t map;
    map = load("map.xml");
    save(map, "map.xm" );


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="6">
<map_var class_id="0" tracking_level="0" version="0">
    <item class_id="1" tracking_level="0" version="0">
        <second class_id="2" tracking_level="0" version="0">
            <name>unknown device</name>

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at