From 944221001c67d01fc528f5e7a0d3b1c9578cec32 Mon Sep 17 00:00:00 2001 From: Denis Dzyubenko Date: Mon, 4 Jun 2012 18:52:36 +0200 Subject: [PATCH] Fixed QJsonObject::find() 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 Reviewed-by: Denis Dzyubenko --- src/corelib/json/qjsonobject.cpp | 1 + tests/auto/corelib/json/tst_qtjson.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index 5439855b90..8801e9c96d 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -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); } diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 00810889bb..5498c890fe 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -589,6 +589,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;