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>
This commit is contained in:
parent
8738f09b9f
commit
1f4b958438
@ -115,6 +115,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
|
\internal
|
||||||
*/
|
*/
|
||||||
@ -126,6 +140,19 @@ QJsonObject::QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object)
|
|||||||
d->ref.ref();
|
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.
|
Destroys the object.
|
||||||
|
@ -44,6 +44,10 @@
|
|||||||
|
|
||||||
#include <QtCore/qjsonvalue.h>
|
#include <QtCore/qjsonvalue.h>
|
||||||
#include <QtCore/qiterator.h>
|
#include <QtCore/qiterator.h>
|
||||||
|
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||||
|
#include <QtCore/qpair.h>
|
||||||
|
#include <initializer_list>
|
||||||
|
#endif
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
@ -55,6 +59,16 @@ class Q_CORE_EXPORT QJsonObject
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QJsonObject();
|
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();
|
||||||
|
|
||||||
QJsonObject(const QJsonObject &other);
|
QJsonObject(const QJsonObject &other);
|
||||||
@ -195,6 +209,7 @@ private:
|
|||||||
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
|
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
|
||||||
|
|
||||||
QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object);
|
QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object);
|
||||||
|
void initialize();
|
||||||
void detach(uint reserve = 0);
|
void detach(uint reserve = 0);
|
||||||
void compact();
|
void compact();
|
||||||
|
|
||||||
|
@ -148,6 +148,7 @@ private Q_SLOTS:
|
|||||||
void longStrings();
|
void longStrings();
|
||||||
|
|
||||||
void arrayInitializerList();
|
void arrayInitializerList();
|
||||||
|
void objectInitializerList();
|
||||||
private:
|
private:
|
||||||
QString testDataDir;
|
QString testDataDir;
|
||||||
};
|
};
|
||||||
@ -2681,22 +2682,72 @@ void tst_QtJson::arrayInitializerList()
|
|||||||
QCOMPARE(QJsonValue(a3[1]), QJsonValue(o));
|
QCOMPARE(QJsonValue(a3[1]), QJsonValue(o));
|
||||||
QCOMPARE(QJsonValue(a3[2]), QJsonValue(a2));
|
QCOMPARE(QJsonValue(a3[2]), QJsonValue(a2));
|
||||||
|
|
||||||
QJsonArray a4 { 1, QJsonArray{1,2,3}, QJsonArray{"hello", 2} };
|
QJsonArray a4 { 1, QJsonArray{1,2,3}, QJsonArray{"hello", 2}, QJsonObject{{"one", 1}} };
|
||||||
QCOMPARE(a4.count(), 3);
|
QCOMPARE(a4.count(), 4);
|
||||||
QCOMPARE(QJsonValue(a4[0]), QJsonValue(1));
|
QCOMPARE(QJsonValue(a4[0]), QJsonValue(1));
|
||||||
|
|
||||||
{
|
{
|
||||||
QJsonArray a41 = a4[1].toArray();
|
QJsonArray a41 = a4[1].toArray();
|
||||||
QJsonArray a42 = a4[2].toArray();
|
QJsonArray a42 = a4[2].toArray();
|
||||||
|
QJsonObject a43 = a4[3].toObject();
|
||||||
QCOMPARE(a41.count(), 3);
|
QCOMPARE(a41.count(), 3);
|
||||||
QCOMPARE(a42.count(), 2);
|
QCOMPARE(a42.count(), 2);
|
||||||
|
QCOMPARE(a43.count(), 1);
|
||||||
|
|
||||||
QCOMPARE(QJsonValue(a41[2]), QJsonValue(3));
|
QCOMPARE(QJsonValue(a41[2]), QJsonValue(3));
|
||||||
QCOMPARE(QJsonValue(a42[1]), QJsonValue(2));
|
QCOMPARE(QJsonValue(a42[1]), QJsonValue(2));
|
||||||
|
QCOMPARE(QJsonValue(a43["one"]), QJsonValue(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#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)
|
QTEST_MAIN(tst_QtJson)
|
||||||
#include "tst_qtjson.moc"
|
#include "tst_qtjson.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user