From d6d7624796471b8296fdfa6492b0570bd78e1d93 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Sat, 4 Jan 2014 16:59:32 -0600 Subject: 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 Reviewed-by: Lars Knoll --- src/corelib/json/qjsonvalue.cpp | 22 +++++++++++++++++++--- src/corelib/json/qjsonvalue.h | 5 +++++ 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'src/corelib/json') 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(); -- cgit v1.2.3