diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2012-01-06 13:36:55 +0100 |
---|---|---|
committer | Jamey Hicks <jamey.hicks@nokia.com> | 2012-01-06 15:34:28 +0100 |
commit | 09e23806546ebeea0f42aa89583f8e188cc06426 (patch) | |
tree | 883d4b0e98a531a9a8661591170e85811aebb30c | |
parent | bce061c8d4732e03e4d3fa121afb3cefed671a98 (diff) |
When insering an existing key, remove old value.
Duplicate keys in json are not allowed, so whenever an already existing key is
inserted into JsonObject, remove the old item first.
Change-Id: I1f0790d5e76553fc26d747ef42212c1d3649d67b
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
-rw-r--r-- | src/qjsonobject.cpp | 4 | ||||
-rw-r--r-- | tests/auto/tst_qtjson.cpp | 10 |
2 files changed, 13 insertions, 1 deletions
diff --git a/src/qjsonobject.cpp b/src/qjsonobject.cpp index 5905e03..2885277 100644 --- a/src/qjsonobject.cpp +++ b/src/qjsonobject.cpp @@ -189,8 +189,10 @@ void JsonObject::insert(const QString &key, const JsonValue &value) int pos = o->insertKey(key); - if (pos < (int)o->length) + if (pos < (int)o->length) { + o->removeItems(pos, 1); ++d->compactionCounter; + } o->reserveSpace(requiredSize, pos, 1); Entry *e = o->entryAt(pos); diff --git a/tests/auto/tst_qtjson.cpp b/tests/auto/tst_qtjson.cpp index 2439a0e..79e5ee1 100644 --- a/tests/auto/tst_qtjson.cpp +++ b/tests/auto/tst_qtjson.cpp @@ -90,6 +90,8 @@ private Q_SLOTS: void compactObject(); void validation(); + + void testDuplicateKeys(); }; TestQtJson::TestQtJson(QObject *parent) : QObject(parent) @@ -1076,6 +1078,14 @@ void TestQtJson::validation() } +void TestQtJson::testDuplicateKeys() +{ + JsonObject obj; + obj.insert(QLatin1String("foo"), QLatin1String("bar")); + obj.insert(QLatin1String("foo"), QLatin1String("zap")); + QCOMPARE(obj.numKeys(), 1); + QCOMPARE(obj.value(QLatin1String("foo")).toString(), QLatin1String("zap")); +} QTEST_MAIN(TestQtJson) #include "tst_qtjson.moc" |