Skip to content

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

License

Notifications You must be signed in to change notification settings

guanxl0525/QSerializer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 are in the examples folder.

Workflow

To get started, include qserializer.h in your code. Next, you need to make a serializable class. For this you have 3 ways

Create serialization class

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

  • QS_FIELD
  • QS_COLLECTION
  • QS_OBJECT
  • QS_COLLECTION_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)

1. Inherit from QSerializer

Inherit from QSerializer, use macro QS_SERIALIZER 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_SERIALIZER
// Create data members to be serialized - you can use this members in code
QS_FIELD(int, age)
QS_COLLECTION(QVector, QString, parents)
};

2. Use macro QS_CLASS

Also you may create serializable classes without inherit QSerializer. For this use macro QS_CLASS. Macro QS_CLASS will generate next methods:

  • QJsonValue toJson()
  • void fromJson(const QJsonValue &)
  • QDomNode toXml()
  • void fromXml(const QDomNode &)
  • const QMetaObject * metaObject() const

In this case you must use Q_GADGET in your class.

class User {
Q_GADGET
QS_CLASS
QS_FIELD(int, age)
QS_COLLECTION(QVector, QString, parents)
};

3. QObject-based class

Requirements

For this case you need to define the methods into class or base class:

  • QJsonValue toJson()
  • void fromJson(const QJsonValue &)
  • QDomNode toXml()
  • void fromXml(const QDomNode &)
  • const QMetaObject * metaObject() const
NOTE: Method metaObject() already exists if you use QObject-based class, however QObject is a heavier parent

You also need to define an assignment operator and a copy constructor, since QObject forbids copying itself by default. This is the most difficult path.

For example:

class User : public QObject
{
Q_OBJECT
// Create data members to be serialized - you can use this members in code
QS_FIELD(int, age)
QS_COLLECTION(QVector, QString, parents)
public:
  User() {...}
  User(const User & u) {...}
  User & operator=(const User & u) {...}
  QJsonValue toJson();
  void fromJson(const QJsonValue &);
  QDomNode toXml();
  void fromXml(const QDomNode &);
};

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);

Detailed description

Macro Description Restrictions For example
QS_FIELD Create simple field, generate methods and propertyes Available types:
  • int
  • QString
  • double
  • bool
  • short
  • unsigned char
  • create field named "Digit" of int type
    QS_FIELD(int, Digit)
    QS_COLLECTION Create collection of simple fields, generate methods and propertyes Available types of collections:
  • QVector
  • QList
  • QStack
  • QQueue
  • create collection named "MyCollection" of QVector<int> type
    QS_COLLECTION(QVector, int, MyCollection)
    QS_OBJECT Create some class object field, generate methods and propertyes You may create some object field, if class of this object provide methods:
  • QJsonValue toJson() const
  • void fromJson(const QJsonValue &)
  • QDomNode toXml() const
  • void fromXml(const QDomNode &)
  • const QMetaObject * metaObject() const
  • create some object "MyObject" of MyClass type
    QS_OBJECT(MyClass, MyObject)
    QS_COLLECTION_OBJECTS Create collection of some objects, generate methods and propertyes You may create collection of objects satisfying restrictions of QS_OBJECT create collection of some objects
    named "MyObjectsCollection" of QList<MyClass> type QS_COLLECTION_OBJECTS(QList, MyClass, MyObjectsCollection)
    QS_CLASS Generate methods for serialization Use this macro if you don't inherit from QSerializer and use Q_GADGET class ClassName {
    Q_GADGET
    QS_CLASS
    ...
    };
    QS_SERIALIZER Override method metaObject Use if you inherits QSerializer class class ClassName : public QSerializer {
    Q_GADGET
    QS_SERIALIZER
    ...
    };
    QS_DECLARE_MEMBER Created new public member of class Use exists types create public member class [int Digit]
    QS_DECLARE_VARIABLE(int, Digit)

    About

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

    Resources

    License

    Stars

    Watchers

    Forks

    Packages

    No packages published

    Languages

    • C++ 95.2%
    • QMake 4.8%