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:
Christian Kandeler 2015-09-21 16:24:41 +02:00 committed by Christian Kandeler
parent 2cd2cba086
commit 4b63ab9a93
2 changed files with 8 additions and 7 deletions

View File

@ -438,7 +438,7 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
The QJsonValue types will be converted as follows: The QJsonValue types will be converted as follows:
\value Null \l {QVariant::}{QVariant()} \value Null QMetaType::Nullptr
\value Bool QMetaType::Bool \value Bool QMetaType::Bool
\value Double QMetaType::Double \value Double QMetaType::Double
\value String QString \value String QString
@ -466,6 +466,7 @@ QVariant QJsonValue::toVariant() const
QJsonObject(d, static_cast<QJsonPrivate::Object *>(base)).toVariantMap() : QJsonObject(d, static_cast<QJsonPrivate::Object *>(base)).toVariantMap() :
QVariantMap(); QVariantMap();
case Null: case Null:
return QVariant::fromValue(nullptr);
case Undefined: case Undefined:
break; break;
} }

View File

@ -1186,14 +1186,14 @@ void tst_QtJson::toVariantMap()
QCOMPARE(map.size(), 3); QCOMPARE(map.size(), 3);
QCOMPARE(map.value("Key"), QVariant(QString("Value"))); 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); QCOMPARE(map.value("Array").type(), QVariant::List);
QVariantList list = map.value("Array").toList(); QVariantList list = map.value("Array").toList();
QCOMPARE(list.size(), 4); QCOMPARE(list.size(), 4);
QCOMPARE(list.at(0), QVariant(true)); QCOMPARE(list.at(0), QVariant(true));
QCOMPARE(list.at(1), QVariant(999.)); QCOMPARE(list.at(1), QVariant(999.));
QCOMPARE(list.at(2), QVariant(QLatin1String("string"))); QCOMPARE(list.at(2), QVariant(QLatin1String("string")));
QCOMPARE(list.at(3), QVariant()); QCOMPARE(list.at(3), QVariant::fromValue(nullptr));
} }
void tst_QtJson::toVariantHash() void tst_QtJson::toVariantHash()
@ -1215,14 +1215,14 @@ void tst_QtJson::toVariantHash()
QCOMPARE(hash.size(), 3); QCOMPARE(hash.size(), 3);
QCOMPARE(hash.value("Key"), QVariant(QString("Value"))); 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); QCOMPARE(hash.value("Array").type(), QVariant::List);
QVariantList list = hash.value("Array").toList(); QVariantList list = hash.value("Array").toList();
QCOMPARE(list.size(), 4); QCOMPARE(list.size(), 4);
QCOMPARE(list.at(0), QVariant(true)); QCOMPARE(list.at(0), QVariant(true));
QCOMPARE(list.at(1), QVariant(999.)); QCOMPARE(list.at(1), QVariant(999.));
QCOMPARE(list.at(2), QVariant(QLatin1String("string"))); QCOMPARE(list.at(2), QVariant(QLatin1String("string")));
QCOMPARE(list.at(3), QVariant()); QCOMPARE(list.at(3), QVariant::fromValue(nullptr));
} }
void tst_QtJson::toVariantList() void tst_QtJson::toVariantList()
@ -1246,14 +1246,14 @@ void tst_QtJson::toVariantList()
QCOMPARE(list.size(), 3); QCOMPARE(list.size(), 3);
QCOMPARE(list[0], QVariant(QString("Value"))); QCOMPARE(list[0], QVariant(QString("Value")));
QCOMPARE(list[1], QVariant()); QCOMPARE(list[1], QVariant::fromValue(nullptr));
QCOMPARE(list[2].type(), QVariant::List); QCOMPARE(list[2].type(), QVariant::List);
QVariantList vlist = list[2].toList(); QVariantList vlist = list[2].toList();
QCOMPARE(vlist.size(), 4); QCOMPARE(vlist.size(), 4);
QCOMPARE(vlist.at(0), QVariant(true)); QCOMPARE(vlist.at(0), QVariant(true));
QCOMPARE(vlist.at(1), QVariant(999.)); QCOMPARE(vlist.at(1), QVariant(999.));
QCOMPARE(vlist.at(2), QVariant(QLatin1String("string"))); QCOMPARE(vlist.at(2), QVariant(QLatin1String("string")));
QCOMPARE(vlist.at(3), QVariant()); QCOMPARE(vlist.at(3), QVariant::fromValue(nullptr));
} }
void tst_QtJson::toJson() void tst_QtJson::toJson()