GithubHelp home page GithubHelp logo

xor-gate / qserializer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from smurfomen/qserializer

0.0 1.0 0.0 9.81 MB

This repo for Qt/C++ serialization objects in JSON or XML based on QtCore

Home Page: https://smurfomen.github.io/QSerializer/

License: MIT License

QMake 3.71% C++ 96.29%

qserializer's Introduction

logo This project is designed to convert data from an object view to JSON or XML and opposite in the Qt/C++ ecosystem. C ++ classes by default do not have the required meta-object information for serializing class fields, but Qt is equipped with its own highly efficient meta-object system. An important feature of the QSerializer is the ability to specify serializable fields of the class without having to serialize the entire class. QSerilaizer generate code and declare Q_PROPERTY for every declared member of class. This is convenient because you do not need to create separate structures or classes for serialization of write some code to serialize every class, it's just included to QSerializer.

Installation

Download repository

$ git clone https://github.com/smurfomen/QSerializer.git

Just include qserializer.h in your project and enjoy simple serialization. qserializer.h located in src folder.
Demo-projects for using QSerializer locate in examples folder.

Workflow

To get started, include qserializer.h in your code.

Create serialization class

For create serializable member of class and generate propertyes, use macro:

  • QS_FIELD
  • QS_COLLECTION
  • QS_OBJECT
  • QS_COLLECTION_OBJECTS
  • QS_QT_DICT
  • QS_QT_DICT_OBJECTS
  • QS_STL_DICT
  • QS_STL_DICT_OBJECTS

If you want only declare exists fields - use macro QS_JSON_FIELD, QS_XML_FIELD, QS_JSON_COLLECTION and other (look at qserializer.h)

Inherit from QSerializer

Inherit from QSerializer, use macro QS_SERIALIZABLE or override metaObject method and declare some serializable fields.
In this case you must use Q_GADGET in your class.

class User : public QSerializer
{
Q_GADGET
QS_SERIALIZABLE
// Create data members to be serialized - you can use this members in code
QS_FIELD(int, age)
QS_COLLECTION(QVector, QString, parents)
};

Serialize

Now you can serialize object of this class to JSON or XML. For example:

User u;
u.age = 20;
u.parents.append("Mary");
u.parents.append("Jeff");
QJsonObject jsonUser = u.toJson();
// or
QDomNode xmlUser = u.toXml();

Deserialize

Opposite of the serialization procedure is the deserialization procedure. You can deserialize object from JSON or XML, declared fields will be modified or resets to default for this type values. For example:

...
User u;
QJsonObject userJson;
u.fromJson(userJson);
//or
QDomNode userXml;
u.fromXml(userXml);

Macro description

Macro Description
QS_FIELD Create serializable simple field
QS_COLLECTION Create serializable collection values of primitive types
QS_OBJECT Create serializable inner custom type object
QS_COLLECTION_OBJECTS Create serializable collection of custom type objects
QS_QT_DICT Create serializable dictionary of primitive type values FOR QT DICTIONARY TYPES
QS_QT_DICT_OBJECTS Create serializable dictionary of custom type values FOR QT DICTIONARY TYPES
QS_STL_DICT Create serializable dictionary of primitive type values FOR STL DICTIONARY TYPES
QS_STL_DICT_OBJECTS Create serializable dictionary of custom type values FOR STL DICTIONARY TYPES
QS_SERIALIZABLE Override method metaObject and make class serializable

qserializer's People

Contributors

smurfomen avatar xor-gate avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.