smf/smfservermodule/util/qjson/src/qobjecthelper.h
changeset 10 1d94eb8df9c2
parent 9 b85b0c039c14
equal deleted inserted replaced
9:b85b0c039c14 10:1d94eb8df9c2
     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 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.
       
     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   * Library General Public License for more details.
       
    14   *
       
    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.
       
    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   *
       
    38   * Suppose the declaration of the Person class looks like this:
       
    39   * \code
       
    40   * class Person : public QObject
       
    41     {
       
    42       Q_OBJECT
       
    43 
       
    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)
       
    48       Q_ENUMS(Gender)
       
    49 
       
    50      public:
       
    51         Person(QObject* parent = 0);
       
    52         ~Person();
       
    53 
       
    54         QString name() const;
       
    55         void setName(const QString& name);
       
    56 
       
    57         int phoneNumber() const;
       
    58         void setPhoneNumber(const int  phoneNumber);
       
    59 
       
    60         enum Gender {Male, Female};
       
    61         void setGender(Gender gender);
       
    62         Gender gender() const;
       
    63 
       
    64         QDate dob() const;
       
    65         void setDob(const QDate& dob);
       
    66 
       
    67       private:
       
    68         QString m_name;
       
    69         int m_phoneNumber;
       
    70         Gender m_gender;
       
    71         QDate m_dob;
       
    72     };
       
    73     \endcode
       
    74 
       
    75     The following code will serialize an instance of Person to JSON :
       
    76 
       
    77     \code
       
    78     Person person;
       
    79     person.setName("Flavio");
       
    80     person.setPhoneNumber(123456);
       
    81     person.setGender(Person::Male);
       
    82     person.setDob(QDate(1982, 7, 12));
       
    83 
       
    84     QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
       
    85     Serializer serializer;
       
    86     qDebug() << serializer.serialize( variant);
       
    87     \endcode
       
    88 
       
    89     The generated output will be:
       
    90     \code
       
    91     { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
       
    92     \endcode
       
    93 
       
    94     It's also possible to initialize a QObject using the values stored inside of
       
    95     a QVariantMap.
       
    96 
       
    97     Suppose you have the following JSON data stored into a QString:
       
    98     \code
       
    99     { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
       
   100     \endcode
       
   101 
       
   102     The following code will initialize an already allocated instance of Person
       
   103     using the JSON values:
       
   104     \code
       
   105     Parser parser;
       
   106     QVariant variant = parser.parse(json);
       
   107 
       
   108     Person person;
       
   109     QObjectHelper::qvariant2qobject(variant.toMap(), &person);
       
   110     \endcode
       
   111 
       
   112     \sa Parser
       
   113     \sa Serializer
       
   114   */
       
   115   class QJSON_EXPORT QObjectHelper {
       
   116     public:
       
   117       QObjectHelper();
       
   118       ~QObjectHelper();
       
   119       
       
   120     /**
       
   121     * This method converts a QObject instance into a QVariantMap.
       
   122     *
       
   123     * @param object The QObject instance to be converted.
       
   124     * @param ignoredProperties Properties that won't be converted.
       
   125     */
       
   126     static QVariantMap qobject2qvariant( const QObject* object,
       
   127                                   const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName"))));
       
   128 
       
   129     /**
       
   130     * This method converts a QVariantMap instance into a QObject
       
   131     *
       
   132     * @param object The QObject instance to be converted.
       
   133     */
       
   134     static void qvariant2qobject(const QVariantMap& variant, QObject* object);
       
   135 
       
   136     private:
       
   137       Q_DISABLE_COPY(QObjectHelper)
       
   138       class QObjectHelperPrivate;
       
   139       QObjectHelperPrivate* const d;
       
   140   };
       
   141 }
       
   142 
       
   143 #endif // QOBJECTHELPER_H