diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2017-04-27 14:43:29 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2017-06-08 11:23:46 +0000 |
commit | a7823b487808a869d4bdc3ff20fddb04967211e1 (patch) | |
tree | b717016a90f67a7d8967601d8245f9e3e7bd87c3 /tests/auto | |
parent | 2db0531a57cf85cf1637c26ff4813f5e8e102902 (diff) |
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>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/json/tst_qtjson.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index b215364f0e..32f68c4292 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -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" |