json: Add operator[] to QJsonValue for implicit object and array access
Saves a lot of manual toArray() and toObject() calls when the JSON structure is usually known anyways. Read only access for now. Change-Id: I5fd787144198e0443e4da285a11ce2597b66f99f Reviewed-by: Lars Knoll <lars.knoll@qt.io>bb10
parent
2db0531a57
commit
a7823b4878
|
|
@ -642,6 +642,58 @@ QJsonObject QJsonValue::toObject() const
|
|||
return toObject(QJsonObject());
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a QJsonValue representing the value for the key \a key.
|
||||
|
||||
Equivalent to calling toObject().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 QJsonValue::operator[](const QString &key) const
|
||||
{
|
||||
if (!isObject())
|
||||
return QJsonValue(QJsonValue::Undefined);
|
||||
|
||||
return toObject().value(key);
|
||||
}
|
||||
|
||||
/*!
|
||||
\overload
|
||||
\since 5.10
|
||||
*/
|
||||
const QJsonValue QJsonValue::operator[](QLatin1String key) const
|
||||
{
|
||||
if (!isObject())
|
||||
return QJsonValue(QJsonValue::Undefined);
|
||||
|
||||
return toObject().value(key);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a QJsonValue representing the value for index \a i.
|
||||
|
||||
Equivalent to calling toArray().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 QJsonValue::operator[](int i) const
|
||||
{
|
||||
if (!isArray())
|
||||
return QJsonValue(QJsonValue::Undefined);
|
||||
|
||||
return toArray().at(i);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns \c true if the value is equal to \a other.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -137,6 +137,10 @@ public:
|
|||
QJsonObject toObject() const;
|
||||
QJsonObject toObject(const QJsonObject &defaultValue) const;
|
||||
|
||||
const QJsonValue operator[](const QString &key) const;
|
||||
const QJsonValue operator[](QLatin1String key) const;
|
||||
const QJsonValue operator[](int i) const;
|
||||
|
||||
bool operator==(const QJsonValue &other) const;
|
||||
bool operator!=(const QJsonValue &other) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -145,6 +145,8 @@ private Q_SLOTS:
|
|||
void parseErrorOffset_data();
|
||||
void parseErrorOffset();
|
||||
|
||||
void implicitValueType();
|
||||
|
||||
private:
|
||||
QString testDataDir;
|
||||
};
|
||||
|
|
@ -2908,5 +2910,33 @@ void tst_QtJson::parseErrorOffset()
|
|||
QCOMPARE(error.offset, errorOffset);
|
||||
}
|
||||
|
||||
void tst_QtJson::implicitValueType()
|
||||
{
|
||||
QJsonObject rootObject{
|
||||
{"object", QJsonObject{{"value", 42}}},
|
||||
{"array", QJsonArray{665, 666, 667}}
|
||||
};
|
||||
|
||||
QJsonValue objectValue = rootObject["object"];
|
||||
QCOMPARE(objectValue["value"].toInt(), 42);
|
||||
QCOMPARE(objectValue["missingValue"], QJsonValue(QJsonValue::Undefined));
|
||||
QCOMPARE(objectValue[123], QJsonValue(QJsonValue::Undefined));
|
||||
QCOMPARE(objectValue["missingValue"].toInt(123), 123);
|
||||
|
||||
QJsonValue arrayValue = rootObject["array"];
|
||||
QCOMPARE(arrayValue[1].toInt(), 666);
|
||||
QCOMPARE(arrayValue[-1], QJsonValue(QJsonValue::Undefined));
|
||||
QCOMPARE(arrayValue["asObject"], QJsonValue(QJsonValue::Undefined));
|
||||
QCOMPARE(arrayValue[-1].toInt(123), 123);
|
||||
|
||||
const QJsonObject constObject = rootObject;
|
||||
QCOMPARE(constObject["object"]["value"].toInt(), 42);
|
||||
QCOMPARE(constObject["array"][1].toInt(), 666);
|
||||
|
||||
QJsonValue objectAsValue(rootObject);
|
||||
QCOMPARE(objectAsValue["object"]["value"].toInt(), 42);
|
||||
QCOMPARE(objectAsValue["array"][1].toInt(), 666);
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QtJson)
|
||||
#include "tst_qtjson.moc"
|
||||
|
|
|
|||
Loading…
Reference in New Issue