diff options
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 62 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 27 |
2 files changed, 76 insertions, 13 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 528ba190d6..248ca4923b 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -201,6 +201,12 @@ static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok) return v_cast<QByteArray>(d)->toLongLong(ok); case QVariant::Bool: return qlonglong(d->data.b); +#ifndef QT_BOOTSTRAPPED + case QMetaType::QJsonValue: + if (!v_cast<QJsonValue>(d)->isDouble()) + break; + // no break +#endif case QVariant::Double: case QVariant::Int: case QMetaType::Char: @@ -209,7 +215,6 @@ static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok) case QMetaType::Long: case QMetaType::Float: case QMetaType::LongLong: - case QMetaType::QJsonValue: return qMetaTypeNumber(d); case QVariant::ULongLong: case QVariant::UInt: @@ -271,6 +276,12 @@ static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok) return v_cast<QByteArray>(d)->toULongLong(ok); case QVariant::Bool: return qulonglong(d->data.b); +#ifndef QT_BOOTSTRAPPED + case QMetaType::QJsonValue: + if (!v_cast<QJsonValue>(d)->isDouble()) + break; + // no break +#endif case QVariant::Double: case QVariant::Int: case QMetaType::Char: @@ -279,7 +290,6 @@ static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok) case QMetaType::Long: case QMetaType::Float: case QMetaType::LongLong: - case QMetaType::QJsonValue: return qulonglong(qMetaTypeNumber(d)); case QVariant::ULongLong: case QVariant::UInt: @@ -413,7 +423,10 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) *str = v_cast<QUrl>(d)->toString(); break; case QMetaType::QJsonValue: - *str = v_cast<QJsonValue>(d)->toString(); + if (v_cast<QJsonValue>(d)->isString()) + *str = v_cast<QJsonValue>(d)->toString(); + else if (!v_cast<QJsonValue>(d)->isNull()) + return false; break; #endif case QVariant::Uuid: @@ -657,7 +670,9 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) break; #ifndef QT_BOOTSTRAPPED case QMetaType::QJsonValue: - *b = v_cast<QJsonValue>(d)->toBool(); + *b = v_cast<QJsonValue>(d)->toBool(false); + if (!v_cast<QJsonValue>(d)->isBool()) + return false; break; #endif default: @@ -698,7 +713,9 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) break; #ifndef QT_BOOTSTRAPPED case QMetaType::QJsonValue: - *f = v_cast<QJsonValue>(d)->toDouble(); + *f = v_cast<QJsonValue>(d)->toDouble(0.0); + if (!v_cast<QJsonValue>(d)->isDouble()) + return false; break; #endif default: @@ -739,7 +756,9 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) break; #ifndef QT_BOOTSTRAPPED case QMetaType::QJsonValue: - *f = v_cast<QJsonValue>(d)->toDouble(); + *f = v_cast<QJsonValue>(d)->toDouble(0.0); + if (!v_cast<QJsonValue>(d)->isDouble()) + return false; break; #endif default: @@ -757,6 +776,14 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) { *static_cast<QVariantList *>(result) = *static_cast<QList<QVariant> *>(d->data.shared->ptr); +#ifndef QT_BOOTSTRAPPED + } else if (d->type == QMetaType::QJsonValue) { + if (!v_cast<QJsonValue>(d)->isArray()) + return false; + *static_cast<QVariantList *>(result) = v_cast<QJsonValue>(d)->toArray().toVariantList(); + } else if (d->type == QMetaType::QJsonArray) { + *static_cast<QVariantList *>(result) = v_cast<QJsonArray>(d)->toVariantList(); +#endif } else { return false; } @@ -765,6 +792,14 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) { *static_cast<QVariantMap *>(result) = *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr); +#ifndef QT_BOOTSTRAPPED + } else if (d->type == QMetaType::QJsonValue) { + if (!v_cast<QJsonValue>(d)->isObject()) + return false; + *static_cast<QVariantMap *>(result) = v_cast<QJsonValue>(d)->toObject().toVariantMap(); + } else if (d->type == QMetaType::QJsonObject) { + *static_cast<QVariantMap *>(result) = v_cast<QJsonObject>(d)->toVariantMap(); +#endif } else { return false; } @@ -773,6 +808,14 @@ static bool convert(const QVariant::Private *d, int t, void *result, bool *ok) if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) { *static_cast<QVariantHash *>(result) = *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr); +#ifndef QT_BOOTSTRAPPED + } else if (d->type == QMetaType::QJsonValue) { + if (!v_cast<QJsonValue>(d)->isObject()) + return false; + *static_cast<QVariantHash *>(result) = v_cast<QJsonValue>(d)->toObject().toVariantHash(); + } else if (d->type == QMetaType::QJsonObject) { + *static_cast<QVariantHash *>(result) = v_cast<QJsonObject>(d)->toVariantHash(); +#endif } else { return false; } @@ -2924,11 +2967,18 @@ bool QVariant::canConvert(int targetTypeId) const case QMetaType::Char: case QMetaType::SChar: case QMetaType::Short: + case QMetaType::QVariantList: + case QMetaType::QVariantMap: + case QMetaType::QVariantHash: return true; default: return false; } } + if (currentType == QMetaType::QJsonArray) + return targetTypeId == QMetaType::QVariantList; + if (currentType == QMetaType::QJsonObject) + return targetTypeId == QMetaType::QVariantMap || targetTypeId == QMetaType::QVariantHash; // FIXME It should be LastCoreType intead of Uuid if (currentType > int(QMetaType::QUuid) || targetTypeId > int(QMetaType::QUuid)) { diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 0ac79d69f3..e95ec88774 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -470,7 +470,13 @@ void tst_QVariant::canConvert_data() << var << N << N << Y << N << Y << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; var = QVariant::fromValue<QJsonValue>(QJsonValue(QStringLiteral("hello"))); QTest::newRow("JsonValue") - << var << N << N << Y << N << N << N << N << N << N << Y << N << N << Y << N << N << N << Y << N << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + << var << N << N << Y << N << N << N << N << N << N << Y << N << N << Y << N << N << Y << Y << Y << N << N << N << N << N << N << N << N << Y << N << N << Y << Y; + var = QVariant::fromValue<QJsonArray>(QJsonArray()); + QTest::newRow("JsonArray") + << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N; + var = QVariant::fromValue<QJsonObject>(QJsonObject()); + QTest::newRow("JsonObject") + << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N << N << N << N << N << N << N << N << N; #undef N #undef Y @@ -526,7 +532,8 @@ void tst_QVariant::toInt_data() bytearray[2] = '0'; bytearray[3] = '0'; QTest::newRow( "QByteArray2" ) << QVariant( bytearray ) << 4500 << true; - QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << 321 << true; + QTest::newRow("int-QJsonValue") << QVariant(QJsonValue(321)) << 321 << true; + QTest::newRow("undefined-QJsonValue") << QVariant(QJsonValue(QJsonValue::Undefined)) << 0 << false; } void tst_QVariant::toInt() @@ -573,7 +580,8 @@ void tst_QVariant::toUInt_data() bytearray[2] = '2'; bytearray[3] = '1'; QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (uint)4321 << true; - QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << (uint)321 << true; + QTest::newRow("int-QJsonValue") << QVariant(QJsonValue(321)) << (uint)321 << true; + QTest::newRow("null-QJsonValue") << QVariant(QJsonValue(QJsonValue::Null)) << (uint)0 << false; } void tst_QVariant::toUInt() @@ -824,7 +832,8 @@ void tst_QVariant::toDouble_data() bytearray[2] = '.'; bytearray[3] = '1'; QTest::newRow( "bytearray" ) << QVariant( bytearray ) << 32.1 << true; - QTest::newRow("QJsonValue") << QVariant(QJsonValue(32.1)) << 32.1 << true; + QTest::newRow("double-QJsonValue") << QVariant(QJsonValue(32.1)) << 32.1 << true; + QTest::newRow("null-QJsonValue") << QVariant(QJsonValue(QJsonValue::Null)) << 0.0 << false; } void tst_QVariant::toDouble() @@ -852,7 +861,8 @@ void tst_QVariant::toFloat_data() bytearray[2] = '.'; bytearray[3] = '1'; QTest::newRow("QByteArray") << QVariant(bytearray) << float(32.1) << true; - QTest::newRow("QJsonValue") << QVariant(QJsonValue(32.1)) << float(32.1) << true; + QTest::newRow("double-QJsonValue") << QVariant(QJsonValue(32.1)) << float(32.1) << true; + QTest::newRow("undefined-QJsonValue") << QVariant(QJsonValue(QJsonValue::Undefined)) << float(0.0) << false; } void tst_QVariant::toFloat() @@ -891,7 +901,8 @@ void tst_QVariant::toLongLong_data() bytearray[2] = '0'; bytearray[3] = '0'; QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qlonglong) 3200 << true; - QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << (qlonglong)321 << true; + QTest::newRow("int-QJsonValue") << QVariant(QJsonValue(321)) << (qlonglong)321 << true; + QTest::newRow("string-QJsonValue") << QVariant(QJsonValue(QString("string"))) << (qlonglong)0 << false; qint64 value64 = (Q_INT64_C(12) << 35) + 8; QTest::newRow("qint64") << QVariant::fromValue(value64) << qlonglong(value64) << true; @@ -947,7 +958,8 @@ void tst_QVariant::toULongLong_data() bytearray[2] = '0'; bytearray[3] = '1'; QTest::newRow( "QByteArray" ) << QVariant( bytearray ) << (qulonglong) 3201 << true; - QTest::newRow("QJsonValue") << QVariant(QJsonValue(321)) << (qulonglong)321 << true; + QTest::newRow("int-QJsonValue") << QVariant(QJsonValue(321)) << (qulonglong)321 << true; + QTest::newRow("bool-QJsonValue") << QVariant(QJsonValue(true)) << (qulonglong)0 << false; quint64 value64 = (Q_INT64_C(12) << 35) + 8; QTest::newRow("qint64") << QVariant::fromValue(value64) << qulonglong(value64) << true; @@ -1024,6 +1036,7 @@ void tst_QVariant::toString_data() QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) << QString( "123456789012" ); QTest::newRow("QJsonValue") << QVariant(QJsonValue(QString("hello"))) << QString("hello"); + QTest::newRow("QJsonValue(Null)") << QVariant(QJsonValue(QJsonValue::Null)) << QString(); } void tst_QVariant::toString() |