diff --git a/src/corelib/serialization/qjson_p.h b/src/corelib/serialization/qjson_p.h index f8f807189f1..37b556b9d01 100644 --- a/src/corelib/serialization/qjson_p.h +++ b/src/corelib/serialization/qjson_p.h @@ -139,7 +139,6 @@ struct ObjectIterator bool operator<=(ObjectIterator other) const { return it <= other.it; } bool operator>=(ObjectIterator other) const { return it >= other.it; } -private: ElementsIterator it; }; diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index ee4ea8ce238..9eeb11cc388 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -310,7 +310,7 @@ static qsizetype indexOf(const QExplicitlySharedDataPointerstringEqualsElement((*it).key(), key); - return (it - begin) * 2; + return it.it - begin.it; } #if QT_STRINGVIEW_LEVEL < 2 @@ -566,7 +566,7 @@ void QJsonObject::removeImpl(T key) if (!keyExists) return; - removeAt(index / 2); + removeAt(index); } #if QT_STRINGVIEW_LEVEL < 2 @@ -619,7 +619,7 @@ QJsonValue QJsonObject::takeImpl(T key) detach(); const QJsonValue v = QJsonPrivate::Value::fromTrustedCbor(o->extractAt(index + 1)); - removeAt(index / 2); + removeAt(index); return v; } @@ -707,7 +707,7 @@ bool QJsonObject::operator!=(const QJsonObject &other) const */ QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it) { - removeAt(it.item.index); + removeAt(it.item.index * 2); // index hasn't changed; the container pointer shouldn't have changed // because we shouldn't have detached (detaching happens on obtaining a @@ -1461,8 +1461,8 @@ void QJsonObject::setValueAt(qsizetype i, const QJsonValue &val) void QJsonObject::removeAt(qsizetype index) { detach(); - o->removeAt(2 * index + 1); - o->removeAt(2 * index); + o->removeAt(index + 1); + o->removeAt(index); } size_t qHash(const QJsonObject &object, size_t seed) diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 98e1782840a..f9b4e9bbd18 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -1023,8 +1023,9 @@ void tst_QtJson::testObjectIteration() for (QJsonObject::iterator it = object.begin(); it != object.end(); ++it) QFAIL("Iterator after property add-and-remove should be empty"); - for (int i = 0; i < 10; ++i) - object[QString::number(i)] = (double)i; + // insert in weird order to confirm keys are sorted + for (int i : {0, 9, 5, 7, 8, 2, 1, 3, 6, 4}) + object[QString::number(i)] = double(i); QCOMPARE(object.size(), 10);