Add initializer list support in QJsonArray

It allows to create a QJsonArray instance in C++ by using
a similar expression to JSON. For example:

QJsonArray a = {1, 2, 4};

[ChangeLog][QtCore][QtJson] QJsonArray now supports
C++11 initializer lists.

Task-number: QTBUG-26606
Change-Id: Icc352e518d9649d24176c89e7113d200d5c50b0d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Jędrzej Nowacki 2014-05-21 12:28:27 +02:00
parent 391b2accd8
commit 9c79f6dfb3
3 changed files with 91 additions and 0 deletions

View File

@ -137,6 +137,18 @@ QJsonArray::QJsonArray()
{
}
/*!
\fn QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args)
\since 5.4
Creates an array initialized from \a args initialization list.
QJsonArray can be constructed in a way similar to JSON notation,
for example:
\code
QJsonArray array = { 1, 2.2, QString() };
\endcode
*/
/*!
\internal
*/
@ -148,6 +160,19 @@ QJsonArray::QJsonArray(QJsonPrivate::Data *data, QJsonPrivate::Array *array)
d->ref.ref();
}
/*!
This method replaces part of QJsonArray(std::initializer_list<QJsonValue> args) .
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 QJsonArray::initialize()
{
d = 0;
a = 0;
}
/*!
Deletes the array.
*/

View File

@ -44,6 +44,9 @@
#include <QtCore/qjsonvalue.h>
#include <QtCore/qiterator.h>
#if defined(Q_COMPILER_INITIALIZER_LISTS)
#include <initializer_list>
#endif
QT_BEGIN_NAMESPACE
@ -56,6 +59,16 @@ class Q_CORE_EXPORT QJsonArray
{
public:
QJsonArray();
#if defined(Q_COMPILER_INITIALIZER_LISTS) || defined(Q_QDOC)
QJsonArray(std::initializer_list<QJsonValue> args)
{
initialize();
for (std::initializer_list<QJsonValue>::const_iterator i = args.begin(); i != args.end(); ++i)
append(*i);
}
#endif
~QJsonArray();
QJsonArray(const QJsonArray &other);
@ -212,6 +225,7 @@ private:
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
QJsonArray(QJsonPrivate::Data *data, QJsonPrivate::Array *array);
void initialize();
void compact();
void detach(uint reserve = 0);

View File

@ -140,6 +140,8 @@ private Q_SLOTS:
void nesting();
void longStrings();
void arrayInitializerList();
private:
QString testDataDir;
};
@ -2443,5 +2445,55 @@ void tst_QtJson::testJsonValueRefDefault()
QCOMPARE(empty["n/a"].toDouble(42.0), 42.0);
}
void tst_QtJson::arrayInitializerList()
{
#ifndef Q_COMPILER_INITIALIZER_LISTS
QSKIP("initializer_list is enabled only with c++11 support");
#else
QVERIFY(QJsonArray{}.isEmpty());
QCOMPARE(QJsonArray{"one"}.count(), 1);
QCOMPARE(QJsonArray{1}.count(), 1);
{
QJsonArray a{1.3, "hello", 0};
QCOMPARE(QJsonValue(a[0]), QJsonValue(1.3));
QCOMPARE(QJsonValue(a[1]), QJsonValue("hello"));
QCOMPARE(QJsonValue(a[2]), QJsonValue(0));
QCOMPARE(a.count(), 3);
}
{
QJsonObject o;
o["property"] = 1;
QJsonArray a1 {o};
QCOMPARE(a1.count(), 1);
QCOMPARE(a1[0].toObject(), o);
QJsonArray a2 {o, 23};
QCOMPARE(a2.count(), 2);
QCOMPARE(a2[0].toObject(), o);
QCOMPARE(QJsonValue(a2[1]), QJsonValue(23));
QJsonArray a3 { a1, o, a2 };
QCOMPARE(QJsonValue(a3[0]), QJsonValue(a1));
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);
QCOMPARE(QJsonValue(a4[0]), QJsonValue(1));
{
QJsonArray a41 = a4[1].toArray();
QJsonArray a42 = a4[2].toArray();
QCOMPARE(a41.count(), 3);
QCOMPARE(a42.count(), 2);
QCOMPARE(QJsonValue(a41[2]), QJsonValue(3));
QCOMPARE(QJsonValue(a42[1]), QJsonValue(2));
}
}
#endif
}
QTEST_MAIN(tst_QtJson)
#include "tst_qtjson.moc"