From b83dc929ec851b81c1236ea4fbeb7f9ad6239565 Mon Sep 17 00:00:00 2001 From: Sze Howe Koh Date: Tue, 4 Mar 2014 20:48:31 +0800 Subject: [PATCH] Complete QJsonValue::fromVariant() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - QVariant can store (U)Int, (U)LongLong, Float and Double numbers. Previously, QJsonValue::fromVariant() converted Floats into Strings while converting the others to Doubles. - Add unit tests for QJsonValue::fromVariant() [ChangeLog][QtCore][JSON] QJsonValue::fromVariant() will now convert single-precision Floats into Doubles instead of Strings Change-Id: I457adbe29c37ada611d1c6d711c42866d63d4024 Reviewed-by: Lars Knoll Reviewed-by: Jędrzej Nowacki --- src/corelib/json/qjsonvalue.cpp | 4 +- tests/auto/corelib/json/tst_qtjson.cpp | 57 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp index e5194f7336..4c4838d314 100644 --- a/src/corelib/json/qjsonvalue.cpp +++ b/src/corelib/json/qjsonvalue.cpp @@ -360,6 +360,7 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other) \li QMetaType::UInt \li QMetaType::LongLong \li QMetaType::ULongLong + \li QMetaType::Float \li QMetaType::Double \endlist \li QJsonValue::Double @@ -391,10 +392,11 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other) */ QJsonValue QJsonValue::fromVariant(const QVariant &variant) { - switch (variant.type()) { + switch (variant.userType()) { case QVariant::Bool: return QJsonValue(variant.toBool()); case QVariant::Int: + case QMetaType::Float: case QVariant::Double: case QVariant::LongLong: case QVariant::ULongLong: diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 589978e1a4..2f75ad631f 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -95,6 +95,7 @@ private Q_SLOTS: void undefinedValues(); + void fromVariant(); void fromVariantMap(); void toVariantMap(); void toVariantList(); @@ -1040,6 +1041,62 @@ void tst_QtJson::undefinedValues() QCOMPARE(array.at(-1).type(), QJsonValue::Undefined); } +void tst_QtJson::fromVariant() +{ + bool boolValue = true; + int intValue = -1; + uint uintValue = 1; + long long longlongValue = -2; + unsigned long long ulonglongValue = 2; + float floatValue = 3.3f; + double doubleValue = 4.4; + QString stringValue("str"); + + QStringList stringList; + stringList.append(stringValue); + stringList.append("str2"); + QJsonArray jsonArray_string; + jsonArray_string.append(stringValue); + jsonArray_string.append("str2"); + + QVariantList variantList; + variantList.append(boolValue); + variantList.append(floatValue); + variantList.append(doubleValue); + variantList.append(stringValue); + variantList.append(stringList); + variantList.append(QVariant()); + QJsonArray jsonArray_variant; + jsonArray_variant.append(boolValue); + jsonArray_variant.append(floatValue); + jsonArray_variant.append(doubleValue); + jsonArray_variant.append(stringValue); + jsonArray_variant.append(jsonArray_string); + jsonArray_variant.append(QJsonValue()); + + QVariantMap variantMap; + variantMap["bool"] = boolValue; + variantMap["float"] = floatValue; + variantMap["string"] = stringValue; + variantMap["array"] = variantList; + QJsonObject jsonObject; + jsonObject["bool"] = boolValue; + jsonObject["float"] = floatValue; + jsonObject["string"] = stringValue; + jsonObject["array"] = jsonArray_variant; + + QCOMPARE(QJsonValue::fromVariant(QVariant(boolValue)), QJsonValue(boolValue)); + QCOMPARE(QJsonValue::fromVariant(QVariant(intValue)), QJsonValue(intValue)); + QCOMPARE(QJsonValue::fromVariant(QVariant(uintValue)), QJsonValue(static_cast(uintValue))); + QCOMPARE(QJsonValue::fromVariant(QVariant(longlongValue)), QJsonValue(longlongValue)); + QCOMPARE(QJsonValue::fromVariant(QVariant(ulonglongValue)), QJsonValue(static_cast(ulonglongValue))); + QCOMPARE(QJsonValue::fromVariant(QVariant(floatValue)), QJsonValue(static_cast(floatValue))); + QCOMPARE(QJsonValue::fromVariant(QVariant(doubleValue)), QJsonValue(doubleValue)); + QCOMPARE(QJsonValue::fromVariant(QVariant(stringValue)), QJsonValue(stringValue)); + QCOMPARE(QJsonValue::fromVariant(QVariant(stringList)), QJsonValue(jsonArray_string)); + QCOMPARE(QJsonValue::fromVariant(QVariant(variantList)), QJsonValue(jsonArray_variant)); + QCOMPARE(QJsonValue::fromVariant(QVariant(variantMap)), QJsonValue(jsonObject)); +} void tst_QtJson::fromVariantMap() {