summaryrefslogtreecommitdiffstats
path: root/src/corelib/json
diff options
context:
space:
mode:
authorMatt Broadstone <mbroadstone@devonit.com>2014-01-04 16:59:32 -0600
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-01-16 21:49:26 +0100
commitd6d7624796471b8296fdfa6492b0570bd78e1d93 (patch)
treea009e8b669d0ed4cc86128fa67a7f9211a5f7434 /src/corelib/json
parent13806e6787502f55754660c6241b31d41e6d9ac7 (diff)
Added constructor to QJsonValue for const char *
For convenience, it reads more easily (and is somewhat expected) to be able to add a string to a QJsonArray like you might with a QVariantList: QJsonArray() << "string". Previously, QJsonValue provided a private void* ctor to explicitly deny this case because it would implicitly convert to a boolean. This ctor provides a const char* ctor (much like QVariant) that interprets the incoming text as utf8 and creates a String type QJsonValue. [ChangeLog][QtCore][QJsonValue] Added constructor to QJsonValue for const char * Change-Id: Icafa954d3da1fb264f9d0fd7cd1a1d2fbbe15095 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.cpp22
-rw-r--r--src/corelib/json/qjsonvalue.h5
2 files changed, 24 insertions, 3 deletions
diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp
index 0a603b958a..c16824ebd2 100644
--- a/src/corelib/json/qjsonvalue.cpp
+++ b/src/corelib/json/qjsonvalue.cpp
@@ -175,7 +175,24 @@ QJsonValue::QJsonValue(qint64 n)
QJsonValue::QJsonValue(const QString &s)
: d(0), t(String)
{
- stringData = *(QStringData **)(&s);
+ stringDataFromQStringHelper(s);
+}
+
+/*!
+ \fn QJsonValue::QJsonValue(const char *s)
+
+ Creates a value of type String with value \a s, assuming
+ UTF-8 encoding of the input.
+
+ You can disable this constructor by defining \c
+ QT_NO_CAST_FROM_ASCII when you compile your applications.
+
+ \since 5.3
+ */
+
+void QJsonValue::stringDataFromQStringHelper(const QString &string)
+{
+ stringData = *(QStringData **)(&string);
stringData->ref.ref();
}
@@ -187,8 +204,7 @@ QJsonValue::QJsonValue(QLatin1String s)
{
// ### FIXME: Avoid creating the temp QString below
QString str(s);
- stringData = *(QStringData **)(&str);
- stringData->ref.ref();
+ stringDataFromQStringHelper(str);
}
/*!
diff --git a/src/corelib/json/qjsonvalue.h b/src/corelib/json/qjsonvalue.h
index c0ecdd2b61..fe028990c0 100644
--- a/src/corelib/json/qjsonvalue.h
+++ b/src/corelib/json/qjsonvalue.h
@@ -82,6 +82,10 @@ public:
QJsonValue(qint64 n);
QJsonValue(const QString &s);
QJsonValue(QLatin1String s);
+#ifndef QT_NO_CAST_FROM_ASCII
+ inline QT_ASCII_CAST_WARN QJsonValue(const char *s)
+ : d(0), t(String) { stringDataFromQStringHelper(QString::fromUtf8(s)); }
+#endif
QJsonValue(const QJsonArray &a);
QJsonValue(const QJsonObject &o);
@@ -123,6 +127,7 @@ private:
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
QJsonValue(QJsonPrivate::Data *d, QJsonPrivate::Base *b, const QJsonPrivate::Value& v);
+ void stringDataFromQStringHelper(const QString &string);
void detach();