Boost将对象序列化为json

时间:2018-02-09 09:15:11

标签: c++ json serialization boost

假设我有一个win32 { DEFINES += LOGFILE_DIR=\\\"C:\\ProgramData\\Foo\\Logs\\ \\\" } 类型的对象,我将其序列化。

在以下示例中,要序列化的内容是

Animal

尼斯。但是,如果我需要将其序列化为22 serialization::archive 16 0 0 4 1 5 Horse ,该怎么办?是否可以通过json序列化?

我寻找这样一个字符串:

boost

代码:

{
  "legs": 4,
  "is_mammal": true,
  "name": "Horse"
}

结果

#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

1 个答案:

答案 0 :(得分:1)

不存在这样的事情。

您可以编写自己的(通过实现存档概念)。但我认为这不值得付出努力。只需使用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;
}

打印

stream print: {"legs":4, "is_mammal":true, "name":"Horse"}
  

CAVEAT

     

我不推荐这种方法。事实上,上面有许多缺失的东西 - 最值得注意的是它只是输出