json: Add operator[] to QJsonDocument for implicit object and array access

Makes it easier to pull out data from a document when the structure is
known up front, while still supporting default values if the structure
does not match the expectation, eg:

 int age = QJsonDocument::fromJson(ba)["users"][0]["age"].toInt(-1);

Change-Id: Ief0899bbb81610f6f22a56e2ac846121bffe77a0
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Tor Arne Vestbø 2017-04-27 14:57:06 +02:00
parent a7823b4878
commit c3a115b90d
3 changed files with 76 additions and 0 deletions

View File

@ -550,6 +550,58 @@ void QJsonDocument::setArray(const QJsonArray &array)
d->ref.ref();
}
/*!
Returns a QJsonValue representing the value for the key \a key.
Equivalent to calling object().value(key).
The returned QJsonValue is QJsonValue::Undefined if the key does not exist,
or if isObject() is false.
\since 5.10
\sa QJsonValue, QJsonValue::isUndefined(), QJsonObject
*/
const QJsonValue QJsonDocument::operator[](const QString &key) const
{
if (!isObject())
return QJsonValue(QJsonValue::Undefined);
return object().value(key);
}
/*!
\overload
\since 5.10
*/
const QJsonValue QJsonDocument::operator[](QLatin1String key) const
{
if (!isObject())
return QJsonValue(QJsonValue::Undefined);
return object().value(key);
}
/*!
Returns a QJsonValue representing the value for index \a i.
Equivalent to calling array().at(i).
The returned QJsonValue is QJsonValue::Undefined, if \a i is out of bounds,
or if isArray() is false.
\since 5.10
\sa QJsonValue, QJsonValue::isUndefined(), QJsonArray
*/
const QJsonValue QJsonDocument::operator[](int i) const
{
if (!isArray())
return QJsonValue(QJsonValue::Undefined);
return array().at(i);
}
/*!
Returns \c true if the \a other document is equal to this document.
*/

View File

@ -148,6 +148,10 @@ public:
void setObject(const QJsonObject &object);
void setArray(const QJsonArray &array);
const QJsonValue operator[](const QString &key) const;
const QJsonValue operator[](QLatin1String key) const;
const QJsonValue operator[](int i) const;
bool operator==(const QJsonDocument &other) const;
bool operator!=(const QJsonDocument &other) const { return !(*this == other); }

View File

@ -146,6 +146,7 @@ private Q_SLOTS:
void parseErrorOffset();
void implicitValueType();
void implicitDocumentType();
private:
QString testDataDir;
@ -2938,5 +2939,24 @@ void tst_QtJson::implicitValueType()
QCOMPARE(objectAsValue["array"][1].toInt(), 666);
}
void tst_QtJson::implicitDocumentType()
{
QJsonDocument emptyDocument;
QCOMPARE(emptyDocument["asObject"], QJsonValue(QJsonValue::Undefined));
QCOMPARE(emptyDocument[123], QJsonValue(QJsonValue::Undefined));
QJsonDocument objectDocument(QJsonObject{{"value", 42}});
QCOMPARE(objectDocument["value"].toInt(), 42);
QCOMPARE(objectDocument["missingValue"], QJsonValue(QJsonValue::Undefined));
QCOMPARE(objectDocument[123], QJsonValue(QJsonValue::Undefined));
QCOMPARE(objectDocument["missingValue"].toInt(123), 123);
QJsonDocument arrayDocument(QJsonArray{665, 666, 667});
QCOMPARE(arrayDocument[1].toInt(), 666);
QCOMPARE(arrayDocument[-1], QJsonValue(QJsonValue::Undefined));
QCOMPARE(arrayDocument["asObject"], QJsonValue(QJsonValue::Undefined));
QCOMPARE(arrayDocument[-1].toInt(123), 123);
}
QTEST_MAIN(tst_QtJson)
#include "tst_qtjson.moc"