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>
This commit is contained in:
Matt Broadstone 2014-01-04 16:59:32 -06:00 committed by The Qt Project
parent 13806e6787
commit d6d7624796
3 changed files with 32 additions and 3 deletions

View File

@ -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);
}
/*!

View File

@ -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();

View File

@ -2242,6 +2242,14 @@ void tst_QtJson::valueEquals()
QVERIFY(QJsonValue(QJsonObject()) != QJsonValue(true));
QVERIFY(QJsonValue(QJsonObject()) != QJsonValue(1.));
QVERIFY(QJsonValue(QJsonObject()) != QJsonValue(QJsonArray()));
QVERIFY(QJsonValue("foo") == QJsonValue(QLatin1String("foo")));
QVERIFY(QJsonValue("foo") == QJsonValue(QString("foo")));
QVERIFY(QJsonValue("\x66\x6f\x6f") == QJsonValue(QString("foo")));
QVERIFY(QJsonValue("\x62\x61\x72") == QJsonValue("bar"));
QVERIFY(QJsonValue(UNICODE_NON_CHARACTER) == QJsonValue(QString(UNICODE_NON_CHARACTER)));
QVERIFY(QJsonValue(UNICODE_DJE) == QJsonValue(QString(UNICODE_DJE)));
QVERIFY(QJsonValue("\xc3\xa9") == QJsonValue(QString("\xc3\xa9")));
}
void tst_QtJson::bom()