summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/corelib/json/qjsonvalue.cpp54
-rw-r--r--src/corelib/json/qjsonvalue.h8
2 files changed, 44 insertions, 18 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,30 +407,54 @@ 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<QJsonPrivate::Array *>(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<QJsonPrivate::Object *>(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.
*/
bool QJsonValue::operator==(const QJsonValue &other) const
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;