linxmouse / QSerializer

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

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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. Set compiler define QS_HAS_JSON or QS_HAS_XML for enabling support for xml or json. Enable both at the same time is not supported.


A demo project for using QSerializer located in example 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");

/* case: json value */
QJsonObject jsonUser = u.toJson();

/* case: raw json data */
QByteArray djson = u.toRawJson();

/* case: xml-dom */
QDomNode xmlUser = u.toXml();

/* case: raw xml data */
QByteArray dxml = u.toRawXml();

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;

/* case: json value */
QJsonObject json;
u.fromJson(json);

/* case: raw json data */
QByteArray rawJson;
u.fromJson(rawJson);

/* case: xml-dom */
QDomNode xml;
u.fromXml(xml);

/* case: raw xml data */
QByteArray rawXml;
u.fromXml(rawXml);

Macro description

Macro Description
QSERIALIZABLE Make class or struct is serializable to QSerializer (override QMetaObject method and define Q_GADGET macro)
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

About

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

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

License:MIT License


Languages

Language:C++ 97.2%Language:QMake 2.8%