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 Lesser General Public
7 * License version 2.1, as published by the Free Software Foundation.
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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser 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
37 * Properties marked as 'non-stored' will be ignored.
39 * Suppose the declaration of the Person class looks like this:
41 * class Person : public QObject
45 Q_PROPERTY(QString name READ name WRITE setName)
46 Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
47 Q_PROPERTY(Gender gender READ gender WRITE setGender)
48 Q_PROPERTY(QDate dob READ dob WRITE setDob)
52 Person(QObject* parent = 0);
56 void setName(const QString& name);
58 int phoneNumber() const;
59 void setPhoneNumber(const int phoneNumber);
61 enum Gender {Male, Female};
62 void setGender(Gender gender);
63 Gender gender() const;
66 void setDob(const QDate& dob);
76 The following code will serialize an instance of Person to JSON :
80 person.setName("Flavio");
81 person.setPhoneNumber(123456);
82 person.setGender(Person::Male);
83 person.setDob(QDate(1982, 7, 12));
85 QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
86 Serializer serializer;
87 qDebug() << serializer.serialize( variant);
90 The generated output will be:
92 { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
95 It's also possible to initialize a QObject using the values stored inside of
98 Suppose you have the following JSON data stored into a QString:
100 { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
103 The following code will initialize an already allocated instance of Person
104 using the JSON values:
107 QVariant variant = parser.parse(json);
110 QObjectHelper::qvariant2qobject(variant.toMap(), &person);
116 class QJSON_EXPORT QObjectHelper {
122 * This method converts a QObject instance into a QVariantMap.
124 * @param object The QObject instance to be converted.
125 * @param ignoredProperties Properties that won't be converted.
127 static QVariantMap qobject2qvariant( const QObject* object,
128 const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName"))));
131 * This method converts a QVariantMap instance into a QObject
133 * @param object The QObject instance to be converted.
135 static void qvariant2qobject(const QVariantMap& variant, QObject* object);
138 Q_DISABLE_COPY(QObjectHelper)
139 class QObjectHelperPrivate;
140 QObjectHelperPrivate* const d;
144 #endif // QOBJECTHELPER_H