Complete QJsonValue::fromVariant()

- 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 <lars.knoll@digia.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
Sze Howe Koh 2014-03-04 20:48:31 +08:00 committed by The Qt Project
parent 0aba7fbb58
commit b83dc929ec
2 changed files with 60 additions and 1 deletions

View File

@ -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:

View File

@ -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<double>(uintValue)));
QCOMPARE(QJsonValue::fromVariant(QVariant(longlongValue)), QJsonValue(longlongValue));
QCOMPARE(QJsonValue::fromVariant(QVariant(ulonglongValue)), QJsonValue(static_cast<double>(ulonglongValue)));
QCOMPARE(QJsonValue::fromVariant(QVariant(floatValue)), QJsonValue(static_cast<double>(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()
{