summaryrefslogtreecommitdiffstats
path: root/src/corelib/json
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@digia.com>2013-08-22 16:25:13 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-08-27 08:01:29 +0200
commit507f1889e2b21d0b3367715dc5e63e124433a992 (patch)
tree11a077cb1988e05ac94f42b2bb4cd7e476997891 /src/corelib/json
parentf73518e7325faf1359b7f52333aa7c11ed393c8c (diff)
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>
Diffstat (limited to 'src/corelib/json')
-rw-r--r--src/corelib/json/qjsonvalue.cpp13
-rw-r--r--src/corelib/json/qjsonvalue.h2
2 files changed, 15 insertions, 0 deletions
diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp
index 3aba6124b8..ef9f057d77 100644
--- a/src/corelib/json/qjsonvalue.cpp
+++ b/src/corelib/json/qjsonvalue.cpp
@@ -449,6 +449,19 @@ bool QJsonValue::toBool(bool defaultValue) const
}
/*!
+ 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.
If type() is not Double, the \a defaultValue will be returned.
diff --git a/src/corelib/json/qjsonvalue.h b/src/corelib/json/qjsonvalue.h
index c8efab5d5f..b18bbde0f7 100644
--- a/src/corelib/json/qjsonvalue.h
+++ b/src/corelib/json/qjsonvalue.h
@@ -103,6 +103,7 @@ public:
inline bool isUndefined() const { return type() == Undefined; }
bool toBool(bool defaultValue = false) const;
+ int toInt(int defaultValue = 0) const;
double toDouble(double defaultValue = 0) const;
QString toString(const QString &defaultValue = QString()) const;
QJsonArray toArray() const;
@@ -158,6 +159,7 @@ public:
inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
inline bool toBool() const { return toValue().toBool(); }
+ inline int toInt() const { return toValue().toInt(); }
inline double toDouble() const { return toValue().toDouble(); }
inline QString toString() const { return toValue().toString(); }
QJsonArray toArray() const;