QJsonValue: fix incorrect to{Array,Object} when the value is empty

This is a repeat of commit de6ced6692
"QCborValue: fix incorrect to{Array,Map} when the value is empty" (6.4),
which fixed the same thing for QCborValue. I've just copied the exact
same implementation onto the QJsonValue functions.

Pick-to: 6.2 6.3 6.4 5.15
Fixes: QTBUG-104085
Change-Id: I175efddd75f24ae59057fffd16f6b257bf7ed36d
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
This commit is contained in:
Thiago Macieira 2022-06-08 09:33:23 -07:00
parent 492b338f57
commit e114fec62a
4 changed files with 30 additions and 12 deletions

View File

@ -119,7 +119,6 @@ QJsonArray::QJsonArray() = default;
QJsonArray::QJsonArray(QCborContainerPrivate *array)
: a(array)
{
Q_ASSERT(array);
}
/*!

View File

@ -103,7 +103,6 @@ QJsonObject::QJsonObject() = default;
QJsonObject::QJsonObject(QCborContainerPrivate *object)
: o(object)
{
Q_ASSERT(o);
}
/*!

View File

@ -708,11 +708,14 @@ QString QJsonValue::toString() const
*/
QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const
{
const auto dd = QJsonPrivate::Value::container(value);
const auto n = QJsonPrivate::Value::valueHelper(value);
if (value.type() != QCborValue::Array || n >= 0 || !dd)
if (!isArray())
return defaultValue;
QCborContainerPrivate *dd = nullptr;
const auto n = QJsonPrivate::Value::valueHelper(value);
const auto container = QJsonPrivate::Value::container(value);
Q_ASSERT(n == -1 || container == nullptr);
if (n < 0)
dd = container;
return QJsonArray(dd);
}
@ -735,11 +738,14 @@ QJsonArray QJsonValue::toArray() const
*/
QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const
{
const auto dd = QJsonPrivate::Value::container(value);
const auto n = QJsonPrivate::Value::valueHelper(value);
if (value.type() != QCborValue::Map || n >= 0 || !dd)
if (!isObject())
return defaultValue;
QCborContainerPrivate *dd = nullptr;
const auto container = QJsonPrivate::Value::container(value);
const auto n = QJsonPrivate::Value::valueHelper(value);
Q_ASSERT(n == -1 || container == nullptr);
if (n < 0)
dd = container;
return QJsonObject(dd);
}

View File

@ -768,15 +768,20 @@ void tst_QtJson::testValueObject()
void tst_QtJson::testValueArray()
{
QJsonArray array;
QJsonArray otherArray = {"wrong value"};
QJsonValue value(array);
QCOMPARE(value.toArray(), array);
QCOMPARE(value.toArray(otherArray), array);
array.append(999.);
array.append(QLatin1String("test"));
array.append(true);
QJsonValue value(array);
value = array;
// if we don't modify the original JsonArray, toArray()
// on the JsonValue should return the same object (non-detached).
QCOMPARE(value.toArray(), array);
QCOMPARE(value.toArray(otherArray), array);
// if we modify the original array, it should detach
array.append(QLatin1String("test"));
@ -786,14 +791,23 @@ void tst_QtJson::testValueArray()
void tst_QtJson::testObjectNested()
{
QJsonObject inner, outer;
QJsonObject otherObject = {{"wrong key", "wrong value"}};
QJsonValue v = inner;
QCOMPARE(v.toObject(), inner);
QCOMPARE(v.toObject(otherObject), inner);
inner.insert("number", 999.);
outer.insert("nested", inner);
// if we don't modify the original JsonObject, value()
// should return the same object (non-detached).
QJsonObject value = outer.value("nested").toObject();
v = value;
QCOMPARE(value, inner);
QCOMPARE(value.value("number").toDouble(), 999.);
QCOMPARE(v.toObject(), inner);
QCOMPARE(v.toObject(otherObject), inner);
QCOMPARE(v["number"].toDouble(), 999.);
// if we modify the original object, it should detach and not
// affect the nested object