Add QJsonValue::toInt().
It's a nice feature to have. MSVC also complains about using doubles to create enum values, so the ugly workaround is: enumValue = MyEnum(qRound(json["myEnumValue"].toDouble())); [ChangeLog][QtCore][QJsonValue]Added QJsonValue::toInt(). Change-Id: I1a200b912abf66b2e96390b1980caff26cfa2685 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
parent
f73518e732
commit
507f1889e2
@ -448,6 +448,19 @@ bool QJsonValue::toBool(bool defaultValue) const
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Converts the value to an int and returns it.
|
||||||
|
|
||||||
|
If type() is not Double or the value is not a whole number,
|
||||||
|
the \a defaultValue will be returned.
|
||||||
|
*/
|
||||||
|
int QJsonValue::toInt(int defaultValue) const
|
||||||
|
{
|
||||||
|
if (t == Double && int(dbl) == dbl)
|
||||||
|
return dbl;
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Converts the value to a double and returns it.
|
Converts the value to a double and returns it.
|
||||||
|
|
||||||
|
@ -103,6 +103,7 @@ public:
|
|||||||
inline bool isUndefined() const { return type() == Undefined; }
|
inline bool isUndefined() const { return type() == Undefined; }
|
||||||
|
|
||||||
bool toBool(bool defaultValue = false) const;
|
bool toBool(bool defaultValue = false) const;
|
||||||
|
int toInt(int defaultValue = 0) const;
|
||||||
double toDouble(double defaultValue = 0) const;
|
double toDouble(double defaultValue = 0) const;
|
||||||
QString toString(const QString &defaultValue = QString()) const;
|
QString toString(const QString &defaultValue = QString()) const;
|
||||||
QJsonArray toArray() const;
|
QJsonArray toArray() const;
|
||||||
@ -158,6 +159,7 @@ public:
|
|||||||
inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
|
inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
|
||||||
|
|
||||||
inline bool toBool() const { return toValue().toBool(); }
|
inline bool toBool() const { return toValue().toBool(); }
|
||||||
|
inline int toInt() const { return toValue().toInt(); }
|
||||||
inline double toDouble() const { return toValue().toDouble(); }
|
inline double toDouble() const { return toValue().toDouble(); }
|
||||||
inline QString toString() const { return toValue().toString(); }
|
inline QString toString() const { return toValue().toString(); }
|
||||||
QJsonArray toArray() const;
|
QJsonArray toArray() const;
|
||||||
|
@ -449,13 +449,13 @@ void tst_QtJson::testObjectSimple()
|
|||||||
void tst_QtJson::testObjectSmallKeys()
|
void tst_QtJson::testObjectSmallKeys()
|
||||||
{
|
{
|
||||||
QJsonObject data1;
|
QJsonObject data1;
|
||||||
data1.insert(QStringLiteral("1"), 123);
|
data1.insert(QStringLiteral("1"), 123.);
|
||||||
QVERIFY(data1.contains(QStringLiteral("1")));
|
QVERIFY(data1.contains(QStringLiteral("1")));
|
||||||
QCOMPARE(data1.value(QStringLiteral("1")).toDouble(), (double)123);
|
QCOMPARE(data1.value(QStringLiteral("1")).toDouble(), (double)123);
|
||||||
data1.insert(QStringLiteral("12"), 133);
|
data1.insert(QStringLiteral("12"), 133.);
|
||||||
QCOMPARE(data1.value(QStringLiteral("12")).toDouble(), (double)133);
|
QCOMPARE(data1.value(QStringLiteral("12")).toDouble(), (double)133);
|
||||||
QVERIFY(data1.contains(QStringLiteral("12")));
|
QVERIFY(data1.contains(QStringLiteral("12")));
|
||||||
data1.insert(QStringLiteral("123"), 323);
|
data1.insert(QStringLiteral("123"), 323.);
|
||||||
QCOMPARE(data1.value(QStringLiteral("12")).toDouble(), (double)133);
|
QCOMPARE(data1.value(QStringLiteral("12")).toDouble(), (double)133);
|
||||||
QVERIFY(data1.contains(QStringLiteral("123")));
|
QVERIFY(data1.contains(QStringLiteral("123")));
|
||||||
QCOMPARE(data1.value(QStringLiteral("123")).type(), QJsonValue::Double);
|
QCOMPARE(data1.value(QStringLiteral("123")).type(), QJsonValue::Double);
|
||||||
@ -670,11 +670,15 @@ void tst_QtJson::testValueRef()
|
|||||||
array.append(1.);
|
array.append(1.);
|
||||||
array.append(2.);
|
array.append(2.);
|
||||||
array.append(3.);
|
array.append(3.);
|
||||||
|
array.append(4);
|
||||||
|
array.append(4.1);
|
||||||
array[1] = false;
|
array[1] = false;
|
||||||
|
|
||||||
QCOMPARE(array.size(), 3);
|
QCOMPARE(array.size(), 5);
|
||||||
QCOMPARE(array.at(0).toDouble(), 1.);
|
QCOMPARE(array.at(0).toDouble(), 1.);
|
||||||
QCOMPARE(array.at(2).toDouble(), 3.);
|
QCOMPARE(array.at(2).toDouble(), 3.);
|
||||||
|
QCOMPARE(array.at(3).toInt(), 4);
|
||||||
|
QCOMPARE(array.at(4).toInt(), 0);
|
||||||
QCOMPARE(array.at(1).type(), QJsonValue::Bool);
|
QCOMPARE(array.at(1).type(), QJsonValue::Bool);
|
||||||
QCOMPARE(array.at(1).toBool(), false);
|
QCOMPARE(array.at(1).toBool(), false);
|
||||||
|
|
||||||
@ -2170,6 +2174,16 @@ void tst_QtJson::valueEquals()
|
|||||||
QVERIFY(QJsonValue(true) != QJsonValue(QJsonArray()));
|
QVERIFY(QJsonValue(true) != QJsonValue(QJsonArray()));
|
||||||
QVERIFY(QJsonValue(true) != QJsonValue(QJsonObject()));
|
QVERIFY(QJsonValue(true) != QJsonValue(QJsonObject()));
|
||||||
|
|
||||||
|
QVERIFY(QJsonValue(1) == QJsonValue(1));
|
||||||
|
QVERIFY(QJsonValue(1) != QJsonValue(2));
|
||||||
|
QVERIFY(QJsonValue(1) == QJsonValue(1.));
|
||||||
|
QVERIFY(QJsonValue(1) != QJsonValue(1.1));
|
||||||
|
QVERIFY(QJsonValue(1) != QJsonValue(QJsonValue::Undefined));
|
||||||
|
QVERIFY(QJsonValue(1) != QJsonValue());
|
||||||
|
QVERIFY(QJsonValue(1) != QJsonValue(true));
|
||||||
|
QVERIFY(QJsonValue(1) != QJsonValue(QJsonArray()));
|
||||||
|
QVERIFY(QJsonValue(1) != QJsonValue(QJsonObject()));
|
||||||
|
|
||||||
QVERIFY(QJsonValue(1.) == QJsonValue(1.));
|
QVERIFY(QJsonValue(1.) == QJsonValue(1.));
|
||||||
QVERIFY(QJsonValue(1.) != QJsonValue(2.));
|
QVERIFY(QJsonValue(1.) != QJsonValue(2.));
|
||||||
QVERIFY(QJsonValue(1.) != QJsonValue(QJsonValue::Undefined));
|
QVERIFY(QJsonValue(1.) != QJsonValue(QJsonValue::Undefined));
|
||||||
|
Loading…
Reference in New Issue
Block a user