]> git.cworth.org Git - apitrace/blob - thirdparty/qjson/qobjecthelper.h
Update bundled QJson
[apitrace] / thirdparty / qjson / qobjecthelper.h
1 /* This file is part of qjson
2   *
3   * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
4   *
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.
8   * 
9   *
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.
14   *
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.
19   */
20
21 #ifndef QOBJECTHELPER_H
22 #define QOBJECTHELPER_H
23
24 #include "qjson_export.h"
25
26 #include <QtCore/QLatin1String>
27 #include <QtCore/QStringList>
28 #include <QtCore/QVariantMap>
29
30 class QObject;
31
32 namespace QJson {
33   /**
34   * @brief Class used to convert QObject into QVariant and vivce-versa.
35   * During these operations only the class attributes defined as properties will
36   * be considered.
37   * Properties marked as 'non-stored' will be ignored.
38   *
39   * Suppose the declaration of the Person class looks like this:
40   * \code
41   * class Person : public QObject
42     {
43       Q_OBJECT
44
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)
49       Q_ENUMS(Gender)
50
51      public:
52         Person(QObject* parent = 0);
53         ~Person();
54
55         QString name() const;
56         void setName(const QString& name);
57
58         int phoneNumber() const;
59         void setPhoneNumber(const int  phoneNumber);
60
61         enum Gender {Male, Female};
62         void setGender(Gender gender);
63         Gender gender() const;
64
65         QDate dob() const;
66         void setDob(const QDate& dob);
67
68       private:
69         QString m_name;
70         int m_phoneNumber;
71         Gender m_gender;
72         QDate m_dob;
73     };
74     \endcode
75
76     The following code will serialize an instance of Person to JSON :
77
78     \code
79     Person person;
80     person.setName("Flavio");
81     person.setPhoneNumber(123456);
82     person.setGender(Person::Male);
83     person.setDob(QDate(1982, 7, 12));
84
85     QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
86     Serializer serializer;
87     qDebug() << serializer.serialize( variant);
88     \endcode
89
90     The generated output will be:
91     \code
92     { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
93     \endcode
94
95     It's also possible to initialize a QObject using the values stored inside of
96     a QVariantMap.
97
98     Suppose you have the following JSON data stored into a QString:
99     \code
100     { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
101     \endcode
102
103     The following code will initialize an already allocated instance of Person
104     using the JSON values:
105     \code
106     Parser parser;
107     QVariant variant = parser.parse(json);
108
109     Person person;
110     QObjectHelper::qvariant2qobject(variant.toMap(), &person);
111     \endcode
112
113     \sa Parser
114     \sa Serializer
115   */
116   class QJSON_EXPORT QObjectHelper {
117     public:
118       QObjectHelper();
119       ~QObjectHelper();
120       
121     /**
122     * This method converts a QObject instance into a QVariantMap.
123     *
124     * @param object The QObject instance to be converted.
125     * @param ignoredProperties Properties that won't be converted.
126     */
127     static QVariantMap qobject2qvariant( const QObject* object,
128                                   const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName"))));
129
130     /**
131     * This method converts a QVariantMap instance into a QObject
132     *
133     * @param object The QObject instance to be converted.
134     */
135     static void qvariant2qobject(const QVariantMap& variant, QObject* object);
136
137     private:
138       Q_DISABLE_COPY(QObjectHelper)
139       class QObjectHelperPrivate;
140       QObjectHelperPrivate* const d;
141   };
142 }
143
144 #endif // QOBJECTHELPER_H