Added convenience methods to QJsonArray for appending QJsonValues

operators for +, +=, and << were added to QJsonArray to make
it easier to work with, and more closely resemble the Qt
container classes

[ChangeLog][QtCore][QJsonArray] Added convenience methods to QJsonArray for appending QJsonValues

Change-Id: I96e0a43015f7c0f980cbbef7f20bd2085ee04795
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
Matt Broadstone 2013-12-29 16:19:13 -05:00 committed by The Qt Project
parent 45673221ae
commit 13806e6787
3 changed files with 48 additions and 0 deletions

View File

@ -188,6 +188,31 @@ QJsonArray &QJsonArray::operator =(const QJsonArray &other)
return *this;
}
/*! \fn QJsonArray &QJsonArray::operator+=(const QJsonValue &value)
Appends \a value to the array, and returns a reference to the array itself.
\since 5.3
\sa append(), operator<<()
*/
/*! \fn QJsonArray QJsonArray::operator+(const QJsonValue &value) const
Returns an array that contains all the items in this array followed
by the provided \a value.
\since 5.3
\sa operator+=()
*/
/*! \fn QJsonArray &QJsonArray::operator<<(const QJsonValue &value)
Appends \a value to the array, and returns a reference to the array itself.
\since 5.3
\sa operator+=(), append()
*/
/*!
Converts the string list \a list to a QJsonArray.

View File

@ -183,6 +183,14 @@ public:
typedef iterator Iterator;
typedef const_iterator ConstIterator;
// convenience
inline QJsonArray operator+(const QJsonValue &v) const
{ QJsonArray n = *this; n += v; return n; }
inline QJsonArray &operator+=(const QJsonValue &v)
{ append(v); return *this; }
inline QJsonArray &operator<< (const QJsonValue &v)
{ append(v); return *this; }
// stl compatibility
inline void push_back(const QJsonValue &t) { append(t); }
inline void push_front(const QJsonValue &t) { prepend(t); }

View File

@ -76,6 +76,7 @@ private Q_SLOTS:
void testObjectNested();
void testArrayNested();
void testArrayNestedEmpty();
void testArrayComfortOperators();
void testObjectNestedEmpty();
void testValueRef();
@ -665,6 +666,20 @@ void tst_QtJson::testObjectNestedEmpty()
QCOMPARE(reconstituted.value("inner2").type(), QJsonValue::Object);
}
void tst_QtJson::testArrayComfortOperators()
{
QJsonArray first;
first.append(123.);
first.append(QLatin1String("foo"));
QJsonArray second = QJsonArray() << 123. << QLatin1String("foo");
QCOMPARE(first, second);
first = first + QLatin1String("bar");
second += QLatin1String("bar");
QCOMPARE(first, second);
}
void tst_QtJson::testValueRef()
{
QJsonArray array;