From 54d9fd61e13f7369c831ab76503f7619d60c0cfd Mon Sep 17 00:00:00 2001 From: Liang Qi Date: Tue, 20 Mar 2012 07:07:03 +0100 Subject: Add default value for getters in QJsonValue Done-with: Debao Zhang Change-Id: I3ddd8dd89dc75d91ac9977bf9b6eb3174d7669e4 Reviewed-by: Thiago Macieira Reviewed-by: Lars Knoll --- src/corelib/json/qjsonvalue.cpp | 54 ++++++++++++++++++++++++---------- src/corelib/json/qjsonvalue.h | 8 +++-- tests/auto/corelib/json/tst_qtjson.cpp | 12 +++++++- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp index e25aac50f2..3be7089049 100644 --- a/src/corelib/json/qjsonvalue.cpp +++ b/src/corelib/json/qjsonvalue.cpp @@ -369,36 +369,36 @@ QJsonValue::Type QJsonValue::type() const /*! Converts the value to a bool and returns it. - If type() is not bool, false will be returned. + If type() is not bool, the defaultValue will be returned. */ -bool QJsonValue::toBool() const +bool QJsonValue::toBool(bool defaultValue) const { if (t != Bool) - return false; + return defaultValue; return b; } /*! Converts the value to a double and returns it. - If type() is not Double, 0. will be returned. + If type() is not Double, the defaultValue will be returned. */ -double QJsonValue::toDouble() const +double QJsonValue::toDouble(double defaultValue) const { if (t != Double) - return 0; + return defaultValue; return dbl; } /*! Converts the value to a QString and returns it. - If type() is not String, a QString() will be returned. + If type() is not String, the defaultValue will be returned. */ -QString QJsonValue::toString() const +QString QJsonValue::toString(const QString &defaultValue) const { if (t != String) - return QString(); + return defaultValue; stringData->ref.ref(); // the constructor below doesn't add a ref. QStringDataPtr holder = { stringData }; return QString(holder); @@ -407,29 +407,53 @@ QString QJsonValue::toString() const /*! Converts the value to an array and returns it. - If type() is not Array, a QJsonArray() will be returned. + If type() is not Array, the defaultValue will be returned. */ -QJsonArray QJsonValue::toArray() const +QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const { if (!d || t != Array) - return QJsonArray(); + return defaultValue; return QJsonArray(d, static_cast(base)); } +/*! + \overload + + Converts the value to an array and returns it. + + If type() is not Array, a QJsonArray() will be returned. + */ +QJsonArray QJsonValue::toArray() const +{ + return toArray(QJsonArray()); +} + /*! Converts the value to an object and returns it. - If type() is not Object, a QJsonObject() will be returned. + If type() is not Object, the defaultValue will be returned. */ -QJsonObject QJsonValue::toObject() const +QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const { if (!d || t != Object) - return QJsonObject(); + return defaultValue; return QJsonObject(d, static_cast(base)); } +/*! + \overload + + Converts the value to an object and returns it. + + If type() is not Object, the QJsonObject() will be returned. + */ +QJsonObject QJsonValue::toObject() const +{ + return toObject(QJsonObject()); +} + /*! Returns true if the value is equal to \a other. */ diff --git a/src/corelib/json/qjsonvalue.h b/src/corelib/json/qjsonvalue.h index 69dcc0640a..386bee25a4 100644 --- a/src/corelib/json/qjsonvalue.h +++ b/src/corelib/json/qjsonvalue.h @@ -103,11 +103,13 @@ public: inline bool isObject() const { return type() == Object; } inline bool isUndefined() const { return type() == Undefined; } - bool toBool() const; - double toDouble() const; - QString toString() const; + bool toBool(bool defaultValue = false) const; + double toDouble(double defaultValue = 0) const; + QString toString(const QString &defaultValue = QString()) const; QJsonArray toArray() const; + QJsonArray toArray(const QJsonArray &defaultValue) const; QJsonObject toObject() const; + QJsonObject toObject(const QJsonObject &defaultValue) const; bool operator==(const QJsonValue &other) const; bool operator!=(const QJsonValue &other) const; diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index bf6a58a24b..73b79b1607 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -154,6 +154,12 @@ void TestQtJson::cleanup() void TestQtJson::testValueSimple() { + QJsonObject object; + object.insert("number", 999.); + QJsonArray array; + for (int i = 0; i < 10; ++i) + array.append((double)i); + QJsonValue value(true); QCOMPARE(value.type(), QJsonValue::Bool); QCOMPARE(value.toDouble(), 0.); @@ -161,12 +167,17 @@ void TestQtJson::testValueSimple() QCOMPARE(value.toBool(), true); QCOMPARE(value.toObject(), QJsonObject()); QCOMPARE(value.toArray(), QJsonArray()); + QCOMPARE(value.toDouble(99.), 99.); + QCOMPARE(value.toString(QString("test")), QString("test")); + QCOMPARE(value.toObject(object), object); + QCOMPARE(value.toArray(array), array); value = 999.; QCOMPARE(value.type(), QJsonValue::Double); QCOMPARE(value.toDouble(), 999.); QCOMPARE(value.toString(), QString()); QCOMPARE(value.toBool(), false); + QCOMPARE(value.toBool(true), true); QCOMPARE(value.toObject(), QJsonObject()); QCOMPARE(value.toArray(), QJsonArray()); @@ -190,7 +201,6 @@ void TestQtJson::testValueSimple() QCOMPARE(value.toBool(), false); QCOMPARE(value.toObject(), QJsonObject()); QCOMPARE(value.toArray(), QJsonArray()); - } void TestQtJson::testNumbers() -- cgit v1.2.3