I cannot serialize boost::rational
. I searched for a boost/serialize/rational.h header but it does not exist.
/usr/include/boost/seriali
Just serialize the numerator and denominator.
Here's the legwork, in semi-generic form (supports archives with named nodes, like XML serialization, too): Live On Coliru
#include
#include
#include
#include
#include
namespace boost { namespace serialization {
template
void save(Archive& ar, ::boost::rational const& r, unsigned /*version*/)
{
int n = r.numerator(), d = r.denominator();
ar & boost::serialization::make_nvp("numerator", n);
ar & boost::serialization::make_nvp("denominator", d);
}
template
void load(Archive& ar, ::boost::rational& r, unsigned /*version*/)
{
int n, d;
ar & boost::serialization::make_nvp("numerator", n);
ar & boost::serialization::make_nvp("denominator", d);
r = ::boost::rational(n, d);
}
} }
BOOST_SERIALIZATION_SPLIT_FREE(boost::rational);
using namespace boost;
#include
#include
int main()
{
rational number(2, 3), other;
std::stringstream ss;
{
archive::xml_oarchive oa(ss);
oa << serialization::make_nvp("rational", number);
}
std::cout << "Serialized: '" << ss.str() << "'\n";
{
archive::xml_iarchive ia(ss);
ia >> serialization::make_nvp("rational", other);
}
std::cout << "Deserialized: " << other;
}
Prints
Serialized: '
2
3
'
Deserialized: 2/3