qobjecthelper.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef QOBJECTHELPER_H
  21. #define QOBJECTHELPER_H
  22. #include <QtCore/QLatin1String>
  23. #include <QtCore/QStringList>
  24. #include <QtCore/QVariantMap>
  25. class QObject;
  26. /**
  27. * @brief Class used to convert QObject into QVariant and vivce-versa.
  28. * During these operations only the class attributes defined as properties will
  29. * be considered.
  30. *
  31. * Suppose the declaration of the Person class looks like this:
  32. * \code
  33. * class Person : public QObject
  34. {
  35. Q_OBJECT
  36. Q_PROPERTY(QString name READ name WRITE setName)
  37. Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
  38. Q_PROPERTY(Gender gender READ gender WRITE setGender)
  39. Q_PROPERTY(QDate dob READ dob WRITE setDob)
  40. Q_ENUMS(Gender)
  41. public:
  42. Person(QObject* parent = 0);
  43. ~Person();
  44. QString name() const;
  45. void setName(const QString& name);
  46. int phoneNumber() const;
  47. void setPhoneNumber(const int phoneNumber);
  48. enum Gender {Male, Female};
  49. void setGender(Gender gender);
  50. Gender gender() const;
  51. QDate dob() const;
  52. void setDob(const QDate& dob);
  53. private:
  54. QString m_name;
  55. int m_phoneNumber;
  56. Gender m_gender;
  57. QDate m_dob;
  58. };
  59. \endcode
  60. The following code will serialize an instance of Person to JSON :
  61. \code
  62. Person person;
  63. person.setName("Flavio");
  64. person.setPhoneNumber(123456);
  65. person.setGender(Person::Male);
  66. person.setDob(QDate(1982, 7, 12));
  67. QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
  68. Serializer serializer;
  69. qDebug() << serializer.serialize( variant);
  70. \endcode
  71. The generated output will be:
  72. \code
  73. { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
  74. \endcode
  75. It's also possible to initialize a QObject using the values stored inside of
  76. a QVariantMap.
  77. Suppose you have the following JSON data stored into a QString:
  78. \code
  79. { "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
  80. \endcode
  81. The following code will initialize an already allocated instance of Person
  82. using the JSON values:
  83. \code
  84. Parser parser;
  85. QVariant variant = parser.parse(json);
  86. Person person;
  87. QObjectHelper::qvariant2qobject(variant.toMap(), &person);
  88. \endcode
  89. \sa Parser
  90. \sa Serializer
  91. */
  92. class QObjectHelper {
  93. public:
  94. QObjectHelper();
  95. ~QObjectHelper();
  96. /**
  97. * This method converts a QObject instance into a QVariantMap.
  98. *
  99. * @param object The QObject instance to be converted.
  100. * @param ignoredProperties Properties that won't be converted.
  101. */
  102. static QVariantMap qobject2qvariant( const QObject* object,
  103. const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName"))));
  104. /**
  105. * This method converts a QVariantMap instance into a QObject
  106. *
  107. * @param object The QObject instance to be converted.
  108. */
  109. static void qvariant2qobject(const QVariantMap& variant, QObject* object);
  110. private:
  111. Q_DISABLE_COPY(QObjectHelper)
  112. class QObjectHelperPrivate;
  113. QObjectHelperPrivate* const d;
  114. };
  115. #endif // QOBJECTHELPER_H