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 +++--- 2 files changed, 44 insertions(+), 18 deletions(-) (limited to 'src') 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; -- cgit v1.2.3