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.
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.
To get started, include qserializer.h in your code.
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, 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)
};
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();
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 |
---|---|
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 |