Let's say I have an object from type Animal
and I serialize it.
In the following example, the content to serialization is
22 serialization::archive 16 0 0 4 1 5 Horse
Nice. But, what if I need it to be serialized as a json
. Is it possible via boost
serialization?
I look for such a string:
{
"legs": 4,
"is_mammal": true,
"name": "Horse"
}
Code:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream>
using namespace boost::archive;
class Animal
{
public:
Animal(){}
void set_leg(int l){legs=l;};
void set_name(std::string s){name=s;};
void set_ismammal(bool b){is_mammal=b;};
void print();
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & legs;
ar & is_mammal;
ar & name;
}
int legs;
bool is_mammal;
std::string name;
};
void Animal::print()
{
std::cout
<<name<<" with "
<<legs<<" legs is "
<<(is_mammal?"":"not ")
<<"a mammal"<<std::endl;
}
void save_obj(const Animal &animal,std::stringstream &stream)
{
text_oarchive oa{stream};
oa << animal;
}
void load_obj(std::stringstream &stream,Animal &animal)
{
text_iarchive ia{stream};
ia >> animal;
}
int main()
{
std::stringstream stream;
Animal animal;
animal.set_name("Horse");
animal.set_leg(4);
animal.set_ismammal(true);
save_obj(animal,stream);
Animal duplicate;
load_obj(stream,duplicate);
std::cout<<"object print: ";
duplicate.print();
std::cout<<"stream print: "<<stream.str()<<std::endl;
}
// g++ -std=c++11 main.cpp -lboost_serialization && ./a.out
Results
object print: Horse with 4 legs is a mammal
stream print: 22 serialization::archive 16 0 0 4 1 5 Horse
No there's not such a thing.
You could write your own (by implementing the Archive concept). But I reckon that's not worth the effort. Just use a JSON library.
Here's a sketch of the minimal output-archive model that works with your sample:
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>
struct MyOArchive {
std::ostream& _os;
MyOArchive(std::ostream& os) : _os(os) {}
using is_saving = boost::true_type;
template <typename T>
MyOArchive& operator<<(boost::serialization::nvp<T> const& wrap) {
save(wrap.name(), wrap.value());
return *this;
}
template <typename T>
MyOArchive& operator<<(T const& value) {
return operator<<(const_cast<T&>(value));
}
template <typename T>
MyOArchive& operator<<(T& value) {
save(value);
return *this;
}
template <typename T> MyOArchive& operator&(T const& v) { return operator<<(v); }
bool first_element = true;
void start_property(char const* name) {
if (!first_element) _os << ", ";
first_element = false;
_os << std::quoted(name) << ":";
}
template <typename T> void save(char const* name, T& b) {
start_property(name);
save(b);
}
void save(bool b) { _os << std::boolalpha << b; }
void save(int i) { _os << i; }
void save(std::string& s) { _os << std::quoted(s); }
template <typename T>
void save(T& v) {
using boost::serialization::serialize;
_os << "{";
first_element = true;
serialize(*this, v, 0u);
_os << "}\n";
first_element = false;
}
};
class Animal {
public:
Animal() {}
void set_leg(int l) { legs = l; };
void set_name(std::string s) { name = s; };
void set_ismammal(bool b) { is_mammal = b; };
void print();
private:
friend class boost::serialization::access;
template <typename Archive> void serialize(Archive &ar, unsigned)
{
ar & BOOST_SERIALIZATION_NVP(legs)
& BOOST_SERIALIZATION_NVP(is_mammal)
& BOOST_SERIALIZATION_NVP(name);
}
int legs;
bool is_mammal;
std::string name;
};
void Animal::print() {
std::cout << name << " with " << legs << " legs is " << (is_mammal ? "" : "not ") << "a mammal" << std::endl;
}
void save_obj(const Animal &animal, std::stringstream &stream) {
MyOArchive oa{ stream };
oa << animal;
}
int main() {
std::stringstream stream;
{
Animal animal;
animal.set_name("Horse");
animal.set_leg(4);
animal.set_ismammal(true);
save_obj(animal, stream);
}
std::cout << "stream print: " << stream.str() << std::endl;
}
Prints
stream print: {"legs":4, "is_mammal":true, "name":"Horse"}
CAVEAT
I do not recommend this approach. In fact there are numerous missing things in the above - most notably the fact that it is output-only
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With