diff options
-rw-r--r-- | src/corelib/json/qjsonobject.cpp | 5 | ||||
-rw-r--r-- | tests/auto/corelib/json/tst_qtjson.cpp | 17 |
2 files changed, 19 insertions, 3 deletions
diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index e595753fec..1bdd872e78 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -390,13 +390,14 @@ QJsonValue QJsonObject::take(const QString &key) if (!keyExists) return QJsonValue(QJsonValue::Undefined); - QJsonPrivate::Entry *e = o->entryAt(index); + QJsonValue v(d, o, o->entryAt(index)->value); + detach(); o->removeItems(index, 1); ++d->compactionCounter; if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) compact(); - return QJsonValue(d, o, e->value); + return v; } /*! diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 82656d6309..4a8ef293ff 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -303,12 +303,27 @@ void tst_QtJson::testObjectSimple() QVERIFY2(!object.contains("boolean"), "key boolean should have been removed"); QJsonValue taken = object.take("value"); -// QCOMPARE(taken, value); + QCOMPARE(taken, value); QVERIFY2(!object.contains("value"), "key value should have been removed"); QString before = object.value("string").toString(); object.insert("string", QString::fromLatin1("foo")); QVERIFY2(object.value("string").toString() != before, "value should have been updated"); + + size = object.size(); + QJsonObject subobject; + subobject.insert("number", 42); + subobject.insert(QLatin1String("string"), QLatin1String("foobar")); + object.insert("subobject", subobject); + QCOMPARE(object.size(), size+1); + QJsonValue subvalue = object.take(QLatin1String("subobject")); + QCOMPARE(object.size(), size); + QCOMPARE(subvalue.toObject(), subobject); + // make object detach by modifying it many times + for (int i = 0; i < 64; ++i) + object.insert(QLatin1String("string"), QLatin1String("bar")); + QCOMPARE(object.size(), size); + QCOMPARE(subvalue.toObject(), subobject); } void tst_QtJson::testObjectSmallKeys() |