JSON/CBOR: fix conversions from QVariant containing longs and qfloat16

[ChangeLog][QtCore][QCborValue] Fixed conversions from QVariant when the
variant contained long, unsigned long, or qfloat16.

[ChangeLog][QtCore][QJsonValue] Fixed conversions from QVariant when the
variant contained long, unsigned long, or qfloat16.

Pick-to: 6.8
Fixes: QTBUG-134756
Change-Id: I08d069dd639c0fc5a15afffd4067762ec94d606d
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit 895b1c0ab358189dd3d7330207bbc69e36427350)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2025-03-17 11:35:50 -07:00 committed by Qt Cherry-pick Bot
parent 469c071d69
commit b758518896
4 changed files with 44 additions and 1 deletions

View File

@ -718,12 +718,15 @@ QCborValue QCborValue::fromVariant(const QVariant &variant)
case QMetaType::UShort:
case QMetaType::Int:
case QMetaType::LongLong:
case QMetaType::Long:
case QMetaType::UInt:
return variant.toLongLong();
case QMetaType::ULong:
case QMetaType::ULongLong:
if (variant.toULongLong() <= static_cast<uint64_t>(std::numeric_limits<qint64>::max()))
return variant.toLongLong();
Q_FALLTHROUGH();
case QMetaType::Float16:
case QMetaType::Float:
case QMetaType::Double:
return variant.toDouble();

View File

@ -484,12 +484,15 @@ QJsonValue QJsonValue::fromVariant(const QVariant &variant)
case QMetaType::UShort:
case QMetaType::Int:
case QMetaType::UInt:
case QMetaType::Long:
case QMetaType::LongLong:
return QJsonValue(variant.toLongLong());
case QMetaType::ULong:
case QMetaType::ULongLong:
if (variant.toULongLong() <= static_cast<uint64_t>(std::numeric_limits<qint64>::max()))
return QJsonValue(variant.toLongLong());
Q_FALLTHROUGH();
case QMetaType::Float16:
case QMetaType::Float:
case QMetaType::Double: {
double v = variant.toDouble();

View File

@ -1654,8 +1654,11 @@ void tst_QtJson::fromVariant_data()
bool boolValue = true;
int intValue = -1;
uint uintValue = 1;
long longValue = -2;
ulong ulongValue = 2;
qlonglong longlongValue = -2;
qulonglong ulonglongValue = 2;
qfloat16 float16Value{2.25f};
float floatValue = 3.3f;
double doubleValue = 4.4;
QString stringValue("str");
@ -1686,21 +1689,27 @@ void tst_QtJson::fromVariant_data()
QVariantMap variantMap;
variantMap["bool"] = boolValue;
variantMap["float16"] = QVariant::fromValue(float16Value);
variantMap["float"] = floatValue;
variantMap["double"] = doubleValue;
variantMap["string"] = stringValue;
variantMap["array"] = variantList;
variantMap["null"] = QVariant::fromValue(nullptr);
variantMap["default"] = QVariant();
QVariantHash variantHash;
variantHash["bool"] = boolValue;
variantHash["float16"] = QVariant::fromValue(float16Value);
variantHash["float"] = floatValue;
variantHash["double"] = doubleValue;
variantHash["string"] = stringValue;
variantHash["array"] = variantList;
variantHash["null"] = QVariant::fromValue(nullptr);
variantHash["default"] = QVariant();
QJsonObject jsonObject;
jsonObject["bool"] = boolValue;
jsonObject["float16"] = float(float16Value);
jsonObject["float"] = floatValue;
jsonObject["double"] = doubleValue;
jsonObject["string"] = stringValue;
jsonObject["array"] = jsonArray_variant;
jsonObject["null"] = QJsonValue::Null;
@ -1711,8 +1720,11 @@ void tst_QtJson::fromVariant_data()
QTest::newRow("bool") << QVariant(boolValue) << QJsonValue(boolValue);
QTest::newRow("int") << QVariant(intValue) << QJsonValue(intValue);
QTest::newRow("uint") << QVariant(uintValue) << QJsonValue(static_cast<qint64>(uintValue));
QTest::newRow("long") << QVariant::fromValue(longValue) << QJsonValue(static_cast<qint64>(longValue));
QTest::newRow("ulong") << QVariant::fromValue(ulongValue) << QJsonValue(static_cast<qint64>(ulongValue));
QTest::newRow("longlong") << QVariant(longlongValue) << QJsonValue(longlongValue);
QTest::newRow("ulonglong") << QVariant(ulonglongValue) << QJsonValue(static_cast<double>(ulonglongValue));
QTest::newRow("float16") << QVariant::fromValue(float16Value) << QJsonValue(float(float16Value));
QTest::newRow("float") << QVariant(floatValue) << QJsonValue(floatValue);
QTest::newRow("double") << QVariant(doubleValue) << QJsonValue(doubleValue);
QTest::newRow("string") << QVariant(stringValue) << QJsonValue(stringValue);

View File

@ -24,7 +24,7 @@ private slots:
void taggedByteArrayToJson_data();
void taggedByteArrayToJson();
void fromVariant_data() { toVariant_data(); }
void fromVariant_data();
void fromVariant();
void fromJson_data();
void fromJson();
@ -182,6 +182,31 @@ void tst_QCborValue_Json::taggedByteArrayToJson()
QCOMPARE(QCborArray({v}).toJsonArray(), QJsonArray({json}));
}
void tst_QCborValue_Json::fromVariant_data()
{
toVariant_data();
auto addIntegral = [](auto number) {
QCborValue cv = qint64(number);
QJsonValue jv = qint64(number);
QVariant vv = QVariant::fromValue(number);
QTest::addRow("%s:%lld", vv.typeName(), qlonglong(number)) << cv << vv << jv;
};
// exercise different QVariant numeric types
addIntegral(short(-1));
addIntegral(ushort(1));
// int already tested
addIntegral(65536U);
addIntegral(-0x7fff'ffffL);
addIntegral(0xffff'ffffUL);
addIntegral(-0x1'0000'0000LL);
addIntegral(0x1000'0000'0000ULL);
QTest::addRow("float:1.875") << QCborValue(1.875) << QVariant::fromValue(1.875f) << QJsonValue(1.875);
QTest::addRow("qfloat16:-0.5") << QCborValue(-0.5) << QVariant::fromValue(qfloat16(-0.5f)) << QJsonValue(-0.5);
}
void tst_QCborValue_Json::fromVariant()
{
QFETCH(QCborValue, v);