Fixed QJsonObject::find()
authorDenis Dzyubenko <denis.dzyubenko@nokia.com>
Mon, 4 Jun 2012 16:52:36 +0000 (18:52 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 6 Jun 2012 00:03:11 +0000 (02:03 +0200)
The function returns mutable iterator on the object that can later be passed to
e.g. erase(), hence it should detach() to be consistent with
QJsonObject::begin() which also detaches.

Change-Id: Id79e8e012fd5469e06b68fbc9eecb7c6848ce9c1
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
src/corelib/json/qjsonobject.cpp
tests/auto/corelib/json/tst_qtjson.cpp

index 5439855..8801e9c 100644 (file)
@@ -452,6 +452,7 @@ QJsonObject::iterator QJsonObject::find(const QString &key)
     int index = o ? o->indexOf(key, &keyExists) : 0;
     if (!keyExists)
         return end();
+    detach();
     return iterator(this, index);
 }
 
index 0081088..5498c89 100644 (file)
@@ -590,6 +590,16 @@ void TestQtJson::testObjectIteration()
     }
 
     {
+        QJsonObject object2 = object;
+        QVERIFY(object == object2);
+
+        QJsonObject::iterator it = object2.find(QString::number(5));
+        object2.erase(it);
+        QCOMPARE(object.size(), 10);
+        QCOMPARE(object2.size(), 9);
+    }
+
+    {
         QJsonObject::Iterator it = object.begin();
         it += 5;
         QCOMPARE(QJsonValue(it.value()).toDouble(), 5.);