Add a null pointer check to QJsonObject::toVariantMap

If the object is null, simply return an empty variant map.
Added another test case checking conversion from QJsonArray
to a QVariantList.

Change-Id: Ieccd163e76630f7db7f41255acd9d1baf66bb38d
Reviewed-by: Jonas Gastal <jgastal@profusion.mobi>
Reviewed-by: Kevin Simons <kevin.simons@nokia.com>
This commit is contained in:
Lars Knoll 2012-01-30 14:37:36 +01:00 committed by Qt by Nokia
parent 34e8dd5d6f
commit a4ab8d0bbc
2 changed files with 39 additions and 4 deletions

View File

@ -158,9 +158,11 @@ QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map)
QVariantMap QJsonObject::toVariantMap() const
{
QVariantMap map;
for (uint i = 0; i < o->length; ++i) {
QJsonPrivate::Entry *e = o->entryAt(i);
map.insert(e->key(), QJsonValue(d, o, e->value).toVariant());
if (o) {
for (uint i = 0; i < o->length; ++i) {
QJsonPrivate::Entry *e = o->entryAt(i);
map.insert(e->key(), QJsonValue(d, o, e->value).toVariant());
}
}
return map;
}

View File

@ -88,6 +88,7 @@ private Q_SLOTS:
void fromVariantMap();
void toVariantMap();
void toVariantList();
void toJson();
void fromJson();
@ -883,6 +884,9 @@ void TestQtJson::fromVariantMap()
void TestQtJson::toVariantMap()
{
QJsonObject object;
QVariantMap map = object.toVariantMap();
QVERIFY(map.isEmpty());
object.insert("Key", QString("Value"));
object.insert("null", QJsonValue());
QJsonArray array;
@ -892,7 +896,7 @@ void TestQtJson::toVariantMap()
array.append(QJsonValue());
object.insert("Array", array);
QVariantMap map = object.toVariantMap();
map = object.toVariantMap();
QCOMPARE(map.size(), 3);
QCOMPARE(map.value("Key"), QVariant(QString("Value")));
@ -906,6 +910,35 @@ void TestQtJson::toVariantMap()
QCOMPARE(list.at(3), QVariant());
}
void TestQtJson::toVariantList()
{
QJsonArray array;
QVariantList list = array.toVariantList();
QVERIFY(list.isEmpty());
array.append(QString("Value"));
array.append(QJsonValue());
QJsonArray inner;
inner.append(true);
inner.append(999.);
inner.append(QLatin1String("string"));
inner.append(QJsonValue());
array.append(inner);
list = array.toVariantList();
QCOMPARE(list.size(), 3);
QCOMPARE(list[0], QVariant(QString("Value")));
QCOMPARE(list[1], QVariant());
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());
}
void TestQtJson::toJson()
{
QJsonObject object;