Boost logo

Boost :

Subject: Re: [boost] Boost::Serialization Combined XML- and Binary-Serialization
From: Daniel Kunz (kunzda_at_[hidden])
Date: 2009-07-19 10:13:17


Hallo,

I found my failure with xml serialization. It works now! Thanks Robert
Ramey!

Now I just have my problems with binary serialization. Can somebody help me
with this?

Best regards.
Daniel

Robert Ramey wrote:

>Try replacing
>> boost::serialization::binary_object bo =
>> boost::serialization::make_binary_object(minute,
>with

> boost::serialization::binary_object bo =
> boost::serialization::make_binary_object(& minute,

>Robert Ramey
>Daniel Kunz wrote:
>> Hello,
>> I try to write an function, which can convert data via the
>> serialization interface
>> XML --> class --> binary AND
>> binary --> class --> XML
>> I read the documentation and in the mailing lists but I could not
>> finish my functions.
>>
>> I have 2 problems:
>> 1) the xml function delivers no xml data in my testfile output
>> 2) I cannot add the binary structure to the existing xml-working
>> classes
>> With the binary serialization I didn't find many informations, but
>> that informations I found I can't use with the classes I prepared for
>> xml serialization
>> Is it possible to combine that 2 serializations
>>
>> Here is my testing code I worked with so far. The finish >coding is
>> similar with that class structure.
>>
>> The class structure:
>>
>> --------------------------------------------------------
>> goal.cpp
>> --------------------------------------------------------
>> #include <string>
>> #include <iostream>
>>
>> using namespace std;
>>
>> #include <boost/serialization/base_object.hpp>
>> #include <boost/serialization/list.hpp>
>> #include <boost/serialization/is_abstract.hpp>
>>
>> #include <boost/archive/xml_iarchive.hpp>
>> #include <boost/archive/xml_oarchive.hpp>
>> #include <boost/archive/binary_iarchive.hpp>
>> #include <boost/archive/binary_oarchive.hpp>
>> #include <boost/serialization/binary_object.hpp>
>> namespace serialization
>> {
>>
>> class Goal
>> {
>> public:
>> enum Scored{home=0, visitors=1};
>> Goal(){};
>> Goal(const string _name, int &_minute, bool &_shootout, Scored
>> &_scored, bool &_penality);
>> ~Goal(){};
>> friend ostream & operator<<(ostream &os, const Goal &_goal);
>> private:
>> friend class boost::serialization::access;
>> template<class Archive>
>> void serialize(Archive & ar, const unsigned int version)
>> {
>> using boost::serialization::make_nvp;
>> ar & make_nvp("Name", name);
>> ar & make_nvp("Minute", minute);
>> ar & make_nvp("Shootout", shootout);
>> ar & make_nvp("Scored", scored);
>> ar & make_nvp("Penality", penality);
>>
>> boost::serialization::binary_object bo =
>> boost::serialization::make_binary_object(minute,
>> sizeof(minute));
>> ar & bo;
>> }
>> string name;
>> int minute;
>> bool shootout;
>> Scored scored;
>> bool penality;
>> };
>> }
>>
>> --------------------------------------------------------
>> stadium.h
>> --------------------------------------------------------
>> #include <string>
>> #include <iostream>
>>
>> using namespace std;
>> #include <boost/serialization/base_object.hpp>
>> #include <boost/serialization/list.hpp>
>> #include <boost/serialization/is_abstract.hpp>
>>
>> #include <boost/archive/xml_iarchive.hpp>
>> #include <boost/archive/xml_oarchive.hpp>
>> #include <boost/archive/binary_iarchive.hpp>
>> #include <boost/archive/binary_oarchive.hpp>
>>
>> namespace serialization
>> {
>>
>> class Stadium
>> {
>> private:
>> friend class boost::serialization::access;
>> template<class Archive>
>> void serialize(Archive & ar, const unsigned int version)
>> {
>> using boost::serialization::make_nvp;
>> ar & make_nvp("Name", name);
>> ar & make_nvp("City", city);
>> ar & make_nvp("Spectators", spectators);
>> }
>> string name;
>> string city;
>> int spectators;
>> public:
>> Stadium(){};
>> Stadium(const string _name, const string _city, int
>> &_spectators); friend ostream & operator<<(ostream &os,
>> Stadium &_stadium); };
>> }
>>
>> --------------------------------------------------------
>> match.h
>> --------------------------------------------------------
>> #include <list>
>> #include <time.h>
>> #include <string>
>> #include <iostream>
>> #include <cstdio>
>>
>> using namespace std;
>>
>> #include <boost/serialization/base_object.hpp>
>> #include <boost/serialization/list.hpp>
>> #include <boost/serialization/is_abstract.hpp>
>>
>> #include <boost/archive/xml_iarchive.hpp>
>> #include <boost/archive/xml_oarchive.hpp>
>> #include <boost/archive/binary_iarchive.hpp>
>> #include <boost/archive/binary_oarchive.hpp>
>> #include <boost/serialization/serialization.hpp>
>>
>> #include "goal.h"
>> #include "stadium.h"
>>
>> namespace serialization
>> {
>>
>> class Match
>> {
>> private:
>> friend class boost::serialization::access;
>> template<class Archive>
>>
>> void serialize(Archive & ar, const unsigned int version)
>> {
>> using boost::serialization::make_nvp;
>> ar & make_nvp("Home", home);
>> ar & make_nvp("Visitors", visitors);
>> ar & make_nvp("Kickoff", kickoff);
>> ar & make_nvp("Goals", goals);
>> ar & make_nvp("Stadium", stadium);
>> }
>> string home;
>> string visitors;
>> time_t kickoff;
>> typedef list<Goal> lGoals;
>> lGoals goals;
>> Stadium stadium;
>> public:
>> Match(){};
>> Match(const string _home, const string _visitors, time_t
>> &_kickoff, Goal *_goals, int size, Stadium &_stadium);
>> Match& operator = (Match &_match);
>> friend ostream & operator<<(ostream &out, Match &_match);
>> };
>> }
>>
>> --------------------------------------------------------
>> xml.cpp
>> --------------------------------------------------------
>> #include <boost/archive/xml_iarchive.hpp>
>> #include <boost/archive/xml_oarchive.hpp>
>>
>> #include <iomanip>
>> #include <iostream>
>> #include <fstream>
>> #include <string>
>>
>> #include "goal.h"
>> #include "stadium.h"
>> #include "match.h"
>>
>> #include <boost/serialization/base_object.hpp>
>> #include <boost/serialization/utility.hpp>
>> #include <boost/serialization/list.hpp>
>> #include <boost/serialization/is_abstract.hpp>
>>
>> using namespace std;
>>
>> void save_xml(const serialization::Match &m, const string &filename)
>> {
>> try
>> {
>> ofstream ofs(filename.c_str());
>> assert(ofs.good());
>> boost::archive::xml_oarchive oa(ofs);
>> oa << boost::serialization::make_nvp("Match", m);
>> }
>> catch(exception &e)
>> {
>> cout << e.what() << endl;
>> }
>> }
>>
>> void restore_xml(serialization::Match &m, const string &filename)
>> {
>> try
>> {
>> ifstream ifs(filename.c_str());
>> boost::archive::xml_iarchive ia(ifs);
>> ia >> boost::serialization::make_nvp("Match", m);
>> }
>> catch(exception &e)
>> {
>> cout << e.what() << endl;
>> }
>> }
>>
>> --------------------------------------------------------
>> binary.cpp
>> --------------------------------------------------------
>> void save_bin(const serialization::Match &m, const string &filename)
>> {
>> try
>> {
>> std::ofstream ofs(filename.c_str());
>> boost::archive::binary_oarchive oa(ofs);
>> //oa << m;
>> }
>> catch(exception &e)
>> {
>> cout << e.what() << endl;
>> }
>> }
>>
>> void restore_bin(serialization::Match &m, const string &filename)
>> {
>> try
>> {
>> std::ifstream ifs(filename.c_str());
>> boost::archive::binary_iarchive ia(ifs);
>> //ia >> m;
>> }
>> catch(exception &e)
>> {
>> cout << e.what() << endl;
>> }
>> }
>>
>>
>> I hope somone can help me. I don't know what I can do to complete that
>> functions.
>>
>> Best regards
>> Daniel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk