From 31ffc7bf2a0efe40116764a8c84939d2df4cdeae Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 30 Jul 2019 10:51:52 +0200 Subject: [PATCH] Fix removal of QJsonObject properties when assigning undefined Commit 8010e906d3612aface0daccde41d1a65fca04b0c accidentally ended up removing the removal-on-undefined-insertion check by calling insertAt instead of insert, which had it. This patch moves the check back into setValueAt. Change-Id: Ic381e284d3da37e31c4eb29f79dfab9c55c2e3e9 Fixes: QTBUG-77204 Reviewed-by: Liang Qi Reviewed-by: Edward Welbourne --- src/corelib/serialization/qjsonobject.cpp | 5 ++++- tests/auto/corelib/serialization/json/tst_qtjson.cpp | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index 99ab25f265..329bc4d2c9 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -1476,7 +1476,10 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val) Q_ASSERT(o && i >= 0 && i < (int)o->length); QJsonPrivate::Entry *e = o->entryAt(i); - insertAt(i, e->key(), val, true); + if (val.t == QJsonValue::Undefined) + removeAt(i); + else + insertAt(i, e->key(), val, true); } /*! diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 7b055b5b63..57aa67c142 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -1132,6 +1132,8 @@ void tst_QtJson::undefinedValues() QJsonObject object; object.insert("Key", QJsonValue(QJsonValue::Undefined)); QCOMPARE(object.size(), 0); + object["Key"] = QJsonValue(QJsonValue::Undefined); + QCOMPARE(object.size(), 0); object.insert("Key", QLatin1String("Value")); QCOMPARE(object.size(), 1);