QJsonValue::toVariant(): Differentiate between null and invalid
Both were mapped to QVariant() before. Instead, use a null pointer QVariant for a null JSON value. [ChangeLog][QtCore][QJsonValue] QJsonValue(Null).toVariant() now returns a QVariant of type QMetaType::Nullptr instead of an invalid QVariant. Task-number: QTBUG-43077 Change-Id: Ife611f418583dbff542210bc8c5cd65201212a6e Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
2cd2cba086
commit
4b63ab9a93
@ -438,7 +438,7 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
|
||||
|
||||
The QJsonValue types will be converted as follows:
|
||||
|
||||
\value Null \l {QVariant::}{QVariant()}
|
||||
\value Null QMetaType::Nullptr
|
||||
\value Bool QMetaType::Bool
|
||||
\value Double QMetaType::Double
|
||||
\value String QString
|
||||
@ -466,6 +466,7 @@ QVariant QJsonValue::toVariant() const
|
||||
QJsonObject(d, static_cast<QJsonPrivate::Object *>(base)).toVariantMap() :
|
||||
QVariantMap();
|
||||
case Null:
|
||||
return QVariant::fromValue(nullptr);
|
||||
case Undefined:
|
||||
break;
|
||||
}
|
||||
|
@ -1186,14 +1186,14 @@ void tst_QtJson::toVariantMap()
|
||||
|
||||
QCOMPARE(map.size(), 3);
|
||||
QCOMPARE(map.value("Key"), QVariant(QString("Value")));
|
||||
QCOMPARE(map.value("null"), QVariant());
|
||||
QCOMPARE(map.value("null"), QVariant::fromValue(nullptr));
|
||||
QCOMPARE(map.value("Array").type(), QVariant::List);
|
||||
QVariantList list = map.value("Array").toList();
|
||||
QCOMPARE(list.size(), 4);
|
||||
QCOMPARE(list.at(0), QVariant(true));
|
||||
QCOMPARE(list.at(1), QVariant(999.));
|
||||
QCOMPARE(list.at(2), QVariant(QLatin1String("string")));
|
||||
QCOMPARE(list.at(3), QVariant());
|
||||
QCOMPARE(list.at(3), QVariant::fromValue(nullptr));
|
||||
}
|
||||
|
||||
void tst_QtJson::toVariantHash()
|
||||
@ -1215,14 +1215,14 @@ void tst_QtJson::toVariantHash()
|
||||
|
||||
QCOMPARE(hash.size(), 3);
|
||||
QCOMPARE(hash.value("Key"), QVariant(QString("Value")));
|
||||
QCOMPARE(hash.value("null"), QVariant());
|
||||
QCOMPARE(hash.value("null"), QVariant::fromValue(nullptr));
|
||||
QCOMPARE(hash.value("Array").type(), QVariant::List);
|
||||
QVariantList list = hash.value("Array").toList();
|
||||
QCOMPARE(list.size(), 4);
|
||||
QCOMPARE(list.at(0), QVariant(true));
|
||||
QCOMPARE(list.at(1), QVariant(999.));
|
||||
QCOMPARE(list.at(2), QVariant(QLatin1String("string")));
|
||||
QCOMPARE(list.at(3), QVariant());
|
||||
QCOMPARE(list.at(3), QVariant::fromValue(nullptr));
|
||||
}
|
||||
|
||||
void tst_QtJson::toVariantList()
|
||||
@ -1246,14 +1246,14 @@ void tst_QtJson::toVariantList()
|
||||
|
||||
QCOMPARE(list.size(), 3);
|
||||
QCOMPARE(list[0], QVariant(QString("Value")));
|
||||
QCOMPARE(list[1], QVariant());
|
||||
QCOMPARE(list[1], QVariant::fromValue(nullptr));
|
||||
QCOMPARE(list[2].type(), QVariant::List);
|
||||
QVariantList vlist = list[2].toList();
|
||||
QCOMPARE(vlist.size(), 4);
|
||||
QCOMPARE(vlist.at(0), QVariant(true));
|
||||
QCOMPARE(vlist.at(1), QVariant(999.));
|
||||
QCOMPARE(vlist.at(2), QVariant(QLatin1String("string")));
|
||||
QCOMPARE(vlist.at(3), QVariant());
|
||||
QCOMPARE(vlist.at(3), QVariant::fromValue(nullptr));
|
||||
}
|
||||
|
||||
void tst_QtJson::toJson()
|
||||
|
Loading…
Reference in New Issue
Block a user