1

my intention is to get input from map<int,std::string> and create the binary archive which the below code is failing to do , is this the right way of creating it? And how to know whether the data is correctly archived or not(desirializing using boost) Do i just need to create a boost::archive::binary_iarchive oa(oss,1); and copy it to oss ?

#include <fstream>
#include <boost/serialization/map.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <string>
#include <iostream>
#include <map>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>

using namespace std;

class MyConnections
{
  public:
    MyConnections()
    {

       e_group.insert( std::make_pair(1,"ETOTO") ) ;
       e_group.insert( std::make_pair(2,"ETOTO") ) ;

   }

 template<class archive>
void serialize(archive& ar, const unsigned int version)
{
    using boost::serialization::make_binary_object;
    ar &  boost::serialization::make_binary_object(e_group);
}


  public:
   typedef   map<int,std::string> groups;
   groups  e_group;
};

int main(int argc, char* argv[])
{
    const MyConnections conn;
    stringstream oss(ios_base::out|ios_base::binary);
    boost::archive::binary_oarchive oa(oss,1);
    oa << boost::serialization::make_binary_object(conn)
    std::cout<<oss.str;
}

Error

main.cpp: In member function 'void MyConnections::serialize(archive&, unsigned int)':
main.cpp:29:63: error: cannot convert 'MyConnections::groups {aka std::map<int, std::__cxx11::basic_string<char> >}' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
         ar &  boost::serialization::make_binary_object(e_group);
                                                               ^
main.cpp: In function 'int main(int, char**)':
main.cpp:42:55: error: cannot convert 'const MyConnections' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
    oa << boost::serialization::make_binary_object(conn)
                                                   ^

1 Answer 1

1

Std output doesn't support random binary output. You'd probably use hex of base64 encoding.

Here's a simplistic approach writing out hex bytes:

#include <sstream>
#include <iostream>
#include <iomanip>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/map.hpp>

using namespace std;

class MyConnections
{
    public:
        MyConnections()
        {

            e_group.insert( std::make_pair(1,"ETOTO") ) ;
            e_group.insert( std::make_pair(2,"ETOTO") ) ;

        }

        template<class archive>
            void serialize(archive& ar, const unsigned int /*version*/) {
                using boost::serialization::make_nvp;
                ar & make_nvp("Connections", e_group);
            }

    public:
        typedef   map<int,std::string> groups;
        groups  e_group;
};

std::string foo() {
    const MyConnections conn;

    stringstream oss(ios_base::out|ios_base::binary);
    boost::archive::binary_oarchive oa(oss);
    oa << boost::serialization::make_nvp("Connections", conn);
    std::string data = oss.str();
    return oss.str();
}

int main()
{
    std::string const data = foo();

    for (uint8_t ch : data) {
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(ch) << " ";
    }
}

Prints:

    00 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 45 54 4f 54 4f 02 00 00 00 05 00 00 00 00 00 00 00 45 54 4f 54 4f 

See it Live On Coliru

NOTE: I think the ,1 parameter to the archive constructor was weird. If you're going to pass flags, use the proper enum values.

Sign up to request clarification or add additional context in comments.

10 Comments

sorry for my understanding issues as i am really new to this, can you please explain me why are u not using make_binary_object here, I would want all the maps data to be a part of binary object and i would also like to desiralize and see if data is correct
Also , assume that the same serialization fuction works for both binary and xml format too, can i use the same serialize function for both xml file and bianry file too ?
Yes. You have to name the values (make_nvp e.g.) for XML though
Well, my requirement is to create binary object and xml too , any good approach for doing it ?
And why is it that you have not considered make_binary_object function? Is that same as make_nvp ?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.