diff options
author | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-05-21 16:22:49 +0200 |
---|---|---|
committer | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-07-30 10:20:28 +0200 |
commit | 1f4b958438a7b9a7f8f3fd8139d10f5adf215a8d (patch) | |
tree | 41e774fafc78bb520e23bfdcd2fafa029ad843f5 | |
parent | 8738f09b9fc1b35e3dc78211368d87069f3071f7 (diff) |
Add initializer list support in QJsonObject.
It allows to create a QJsonObject instance in C++ by using
initializer list of pairs QString QJsonValue, for example:
QJsonObject o = {{"property1", 1}, {"property2", 2}};
[ChangeLog][QtCore][QtJson] QJsonObject now supports
C++11 initializer lists.
Task-number: QTBUG-26606
Change-Id: I67af881e175f427e563e685336c48a5f8466b476
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/json/qjsonobject.cpp | 27 | ||||
-rw-r--r-- | src/corelib/json/qjsonobject.h | 15 | ||||
-rw-r--r-- | tests/auto/corelib/json/tst_qtjson.cpp | 55 |
3 files changed, 95 insertions, 2 deletions
diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index 94ec9a2818..cfd797990f 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -116,6 +116,20 @@ QJsonObject::QJsonObject() } /*! + \fn QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) + \since 5.4 + Constructs a QJsonObject instance initialized from \a args initialization list. + For example: + \code + QJsonObject object + { + {"property1", 1}, + {"property2", 2} + }; + \endcode +*/ + +/*! \internal */ QJsonObject::QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object) @@ -126,6 +140,19 @@ QJsonObject::QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object) d->ref.ref(); } +/*! + This method replaces part of the QJsonObject(std::initializer_list<QPair<QString, QJsonValue>> args) body. + The constructor needs to be inline, but we do not want to leak implementation details + of this class. + \note this method is called for an uninitialized object + \internal + */ + +void QJsonObject::initialize() +{ + d = 0; + o = 0; +} /*! Destroys the object. diff --git a/src/corelib/json/qjsonobject.h b/src/corelib/json/qjsonobject.h index ad3184b1f2..92dd19af5e 100644 --- a/src/corelib/json/qjsonobject.h +++ b/src/corelib/json/qjsonobject.h @@ -44,6 +44,10 @@ #include <QtCore/qjsonvalue.h> #include <QtCore/qiterator.h> +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include <QtCore/qpair.h> +#include <initializer_list> +#endif QT_BEGIN_NAMESPACE @@ -55,6 +59,16 @@ class Q_CORE_EXPORT QJsonObject { public: QJsonObject(); + +#if defined(Q_COMPILER_INITIALIZER_LISTS) || defined(Q_QDOC) + QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) + { + initialize(); + for (std::initializer_list<QPair<QString, QJsonValue> >::const_iterator i = args.begin(); i != args.end(); ++i) + insert(i->first, i->second); + } +#endif + ~QJsonObject(); QJsonObject(const QJsonObject &other); @@ -195,6 +209,7 @@ private: friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &); QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object); + void initialize(); void detach(uint reserve = 0); void compact(); diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index c7590fd023..ebe7333c47 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -148,6 +148,7 @@ private Q_SLOTS: void longStrings(); void arrayInitializerList(); + void objectInitializerList(); private: QString testDataDir; }; @@ -2681,22 +2682,72 @@ void tst_QtJson::arrayInitializerList() QCOMPARE(QJsonValue(a3[1]), QJsonValue(o)); QCOMPARE(QJsonValue(a3[2]), QJsonValue(a2)); - QJsonArray a4 { 1, QJsonArray{1,2,3}, QJsonArray{"hello", 2} }; - QCOMPARE(a4.count(), 3); + QJsonArray a4 { 1, QJsonArray{1,2,3}, QJsonArray{"hello", 2}, QJsonObject{{"one", 1}} }; + QCOMPARE(a4.count(), 4); QCOMPARE(QJsonValue(a4[0]), QJsonValue(1)); { QJsonArray a41 = a4[1].toArray(); QJsonArray a42 = a4[2].toArray(); + QJsonObject a43 = a4[3].toObject(); QCOMPARE(a41.count(), 3); QCOMPARE(a42.count(), 2); + QCOMPARE(a43.count(), 1); QCOMPARE(QJsonValue(a41[2]), QJsonValue(3)); QCOMPARE(QJsonValue(a42[1]), QJsonValue(2)); + QCOMPARE(QJsonValue(a43["one"]), QJsonValue(1)); } } #endif } +void tst_QtJson::objectInitializerList() +{ +#ifndef Q_COMPILER_INITIALIZER_LISTS + QSKIP("initializer_list is enabled only with c++11 support"); +#else + QVERIFY(QJsonObject{}.isEmpty()); + + { // one property + QJsonObject one {{"one", 1}}; + QCOMPARE(one.count(), 1); + QVERIFY(one.contains("one")); + QCOMPARE(QJsonValue(one["one"]), QJsonValue(1)); + } + { // two properties + QJsonObject two { + {"one", 1}, + {"two", 2} + }; + QCOMPARE(two.count(), 2); + QVERIFY(two.contains("one")); + QVERIFY(two.contains("two")); + QCOMPARE(QJsonValue(two["one"]), QJsonValue(1)); + QCOMPARE(QJsonValue(two["two"]), QJsonValue(2)); + } + { // nested object + QJsonObject object{{"nested", QJsonObject{{"innerProperty", 2}}}}; + QCOMPARE(object.count(), 1); + QVERIFY(object.contains("nested")); + QVERIFY(object["nested"].isObject()); + + QJsonObject nested = object["nested"].toObject(); + QCOMPARE(QJsonValue(nested["innerProperty"]), QJsonValue(2)); + } + { // nested array + QJsonObject object{{"nested", QJsonArray{"innerValue", 2.1, "bum cyk cyk"}}}; + QCOMPARE(object.count(), 1); + QVERIFY(object.contains("nested")); + QVERIFY(object["nested"].isArray()); + + QJsonArray nested = object["nested"].toArray(); + QCOMPARE(nested.count(), 3); + QCOMPARE(QJsonValue(nested[0]), QJsonValue("innerValue")); + QCOMPARE(QJsonValue(nested[1]), QJsonValue(2.1)); + } +#endif +} + QTEST_MAIN(tst_QtJson) #include "tst_qtjson.moc" |