Added missing detach() to QJsonObject::take()

We also need to detach() the taken value in case the compaction triggers and
modifies the underlaying data.

Change-Id: Idcdeba4236b8e208d107d41be2decbdfc5721300
Reviewed-by: Bjørn Erik Nilsen <post@bjoernen.com>
Reviewed-by: Denis Dzyubenko <denis@ddenis.info>
This commit is contained in:
Denis Dzyubenko 2012-06-04 18:48:51 +02:00 committed by The Qt Project
parent 20d6a0bfbc
commit 6b5b5d4e53
2 changed files with 19 additions and 3 deletions

View File

@ -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;
}
/*!

View File

@ -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()