Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

firemiles/Serialization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serialization

This is a very easy serialization template lib, just need to include header files into project.

Note: this lib depends on Qt5 and c++11

you can serialize object like this:

QByteArray bytes = serialization::Serialize2Json(object).toJson();

and unserialize json to object like this:

TypeOfObject object = serialization::Unserialize4String<TypeOfObject>(bytes);

you should define two interface for custom type.

QJsonObject _Serialize2Json(CustomType const& e);
CustomType _Unserialize4String(QJsonValue const& json, CustomType const& type_traits);

Sample

#include "../../include/serialization.h"
#include <QDebug>

class employee{

public:
    employee(QString const& name=""):name_(name){}
    void add_tag(QString const& tag){ tags_.insert(tag); }
    void info() const {
        qDebug()<<"name:"<<name_;
        qDebug()<<"tags:"<<tags_;
    }
private:
    QString name_;
    QSet<QString> tags_;

    friend QJsonObject _Serialize2Json(employee const& e);
    friend employee _Unserialize4String(QJsonValue const& json, employee const& type_traits);
};

inline QJsonObject _Serialize2Json(employee const& e) {
    QJsonObject obj;
    obj["tags"] = serialization::_Serialize2Json(e.tags_);
    obj["name"] = e.name_;
    return obj;
}

inline employee _Unserialize4String(QJsonValue const& json, employee const& type_traits) {
    QJsonObject obj = json.toObject();
    employee e(obj["name"].toString());
    e.tags_ = serialization::_Unserialize4String(obj["tags"], QSet<QString>());
    return e;
}

class employer{
public:
    employer(QString const& name=""):name_(name){}
    void add_employee(employee const& e){ employees_.append(e);}
    void info() const {
        qDebug()<<"name:"<<name_;
        for(const employee &e:employees_) {
            e.info();
        }
    }

private:
    QVector<employee> employees_;
    QString name_;

    friend QJsonObject _Serialize2Json(employer const& e);
    friend employer _Unserialize4String(QJsonValue const& json, employer const& type_traits);
};

inline QJsonObject _Serialize2Json(employer const& e) {
    QJsonObject obj;
    obj["emplyees"] = serialization::_Serialize2Json(e.employees_);
    obj["name"] = e.name_;
    return obj;
}

inline employer _Unserialize4String(QJsonValue const& json, employer const& type_traits) {
    QJsonObject obj = json.toObject();
    employer e(obj["name"].toString());
    e.employees_ = serialization::_Unserialize4String(obj["emplyees"], QVector<employee>());
    return e;
}

int main(int argc, char *argv[])
{
    employee Mike("Mike"), Jack("Jack");
    Mike.add_tag("man");
    Jack.add_tag("engineer");
    employer Miles("Miles");
    Miles.add_employee(Mike);
    Miles.add_employee(Jack);
    QJsonDocument doc = serialization::Serialize2Json(Miles);
    QByteArray bytes = doc.toJson(QJsonDocument::Compact);
    qDebug()<<bytes;
    employer er = serialization::Unserialize4String<employer>(bytes);
    er.info();

}

About

Easily serialize and unserialize objects between json.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published