4

If I'm using Boost Serialization to serialize an Integer:

#include <boost/archive/text_oarchive.hpp> 
#include <iostream> 

int main() 
{ 
  boost::archive::text_oarchive oa(std::cout); 
  int i = 1; 
  oa << i; 
}

The result will be the following:
22 serialization::archive 5 1

Now I'm curious if and how I could change the way, certain data is serialized. The data does not need to be deserialized, so if that is not possible anymore, it's not a hindering reason to not doing that.

Lets say the above code should create the following output:
integer 11
(The word integer is added and the value will be increased by 10. The archive-header will not be integrated.)

Would that be possible and how could one achieve that? Is Boost Serialization able to let a user do that without modifying the codebase of Serialization?

PS:
The example-code above is copied from the Highscore-Tutorial

5
  • What is the purpose of serializing if there will not be a need to deserialize? Commented Oct 15, 2010 at 18:46
  • Another application will deserialize the data. This app needs the data in a specific format which I want to serialize with Boost Serialize. The idea is that a user later can select between serveral ways of serialization (according to the app he wants to send to). The developer just needs to implement one serialize-method. But I'm currently designing and testing how the concept could work. Commented Oct 17, 2010 at 11:57
  • I see. Well you can instruct Boost.Serialization to not add headers like 22 serialization::archive and you can create your own archive that writes the data in just the format you wish. Commented Oct 18, 2010 at 5:58
  • 1
    Or as you need to comply to a predefined format, you might find Boost.Karma useful? Commented Oct 18, 2010 at 6:01
  • Thanks for that pointer, i'll keep it in mind. Commented Oct 18, 2010 at 6:42

1 Answer 1

5

You can write your own archive, something like this:

#include <cstddef> // std::size_t
#include <string>
#include <typeid>

template <typename T>
std::string printName() {
  // Unmangle for your platform or just specialise for types you care about:
  return typeid(T).name();
}

//////////////////////////////////////////////////////////////
// class trivial_oarchive
class trivial_oarchive {
public:
    //////////////////////////////////////////////////////////
    // public interface used by programs that use the
    // serialization library
    typedef boost::mpl::bool_<true> is_saving; 
    typedef boost::mpl::bool_<false> is_loading;
    template<class T> register_type(){}
    template<class T> trivial_oarchive & operator<<(const T & t){
        return *this;
    }
    template<class T> trivial_oarchive & operator&(const T & t){
        // No idea why you'd want to add 10, but this does it:
        return *this << printName<T>() << " " << (t+10);
    }
    void save_binary(void *address, std::size_t count){};
};

(Adapted from the documentation)

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

Comments

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.