C++ Boost将对象序列化为json
假设我有一个来自typeC++ Boost将对象序列化为json,c++,json,serialization,boost,C++,Json,Serialization,Boost,假设我有一个来自typeAnimal的对象,并将其序列化 在下面的示例中,要序列化的内容是 22 serialization::archive 16 0 0 4 1 5 Horse 很好。但是,如果我需要将其序列化为json,该怎么办呢。是否可以通过boost序列化 我寻找这样一条线: { "legs": 4, "is_mammal": true, "name": "Horse" } 代码: 不,没有这样的事 您可以编写自己的(通过实现归档概念)。但我认为这不值得努力。只需使用J
Animal
的对象,并将其序列化
在下面的示例中,要序列化的内容是
22 serialization::archive 16 0 0 4 1 5 Horse
很好。但是,如果我需要将其序列化为json
,该怎么办呢。是否可以通过boost
序列化
我寻找这样一条线:
{
"legs": 4,
"is_mammal": true,
"name": "Horse"
}
代码:
不,没有这样的事 您可以编写自己的(通过实现归档概念)。但我认为这不值得努力。只需使用JSON库 下面是与您的示例一起使用的最小输出归档模型的草图:
#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;
}
警告
我不推荐这种方法。事实上,上面有许多遗漏的东西——最明显的是,它只是输出
查看:boost::property_tree::write_json@FrankS101,经常使用它。但是如何将它与
存档连接起来呢
?非常感谢您花费的时间。除了创建一个boost树,然后将其序列化为json之外,我还考虑了一个类似的想法来处理更糟糕的情况。
object print: Horse with 4 legs is a mammal
stream print: 22 serialization::archive 16 0 0 4 1 5 Horse
#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;
}
stream print: {"legs":4, "is_mammal":true, "name":"Horse"}