1 /* This file is part of qjson
3 * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #ifndef QOBJECTHELPER_H
22 #define QOBJECTHELPER_H
24 #include "qjson_export.h"
26 #include <QtCore/QLatin1String>
27 #include <QtCore/QStringList>
28 #include <QtCore/QVariantMap>
34 * @brief Class used to convert QObject into QVariant and vivce-versa.
35 * During these operations only the class attributes defined as properties will
38 * Suppose the declaration of the Person class looks like this:
40 * class Person : public QObject
44 Q_PROPERTY(QString name READ name WRITE setName)
45 Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
46 Q_PROPERTY(Gender gender READ gender WRITE setGender)
47 Q_PROPERTY(QDate dob READ dob WRITE setDob)
51 Person(QObject* parent = 0);
55 void setName(const QString& name);
57 int phoneNumber() const;
58 void setPhoneNumber(const int phoneNumber);
60 enum Gender {Male, Female};
61 void setGender(Gender gender);
62 Gender gender() const;
65 void setDob(const QDate& dob);
75 The following code will serialize an instance of Person to JSON :
79 person.setName("Flavio");
80 person.setPhoneNumber(123456);
81 person.setGender(Person::Male);
82 person.setDob(QDate(1982, 7, 12));
84 QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
85 Serializer serializer;
86 qDebug() << serializer.serialize( variant);
89 The generated output will be:
91 { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
94 It's also possible to initialize a QObject using the values stored inside of
97 Suppose you have the following JSON data stored into a QString:
99 { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
102 The following code will initialize an already allocated instance of Person
103 using the JSON values:
106 QVariant variant = parser.parse(json);
109 QObjectHelper::qvariant2qobject(variant.toMap(), &person);
115 class QJSON_EXPORT QObjectHelper {
121 * This method converts a QObject instance into a QVariantMap.
123 * @param object The QObject instance to be converted.
124 * @param ignoredProperties Properties that won't be converted.
126 static QVariantMap qobject2qvariant( const QObject* object,
127 const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName"))));
130 * This method converts a QVariantMap instance into a QObject
132 * @param object The QObject instance to be converted.
134 static void qvariant2qobject(const QVariantMap& variant, QObject* object);
137 Q_DISABLE_COPY(QObjectHelper)
138 class QObjectHelperPrivate;
139 QObjectHelperPrivate* const d;
143 #endif // QOBJECTHELPER_H