diff options
| author | Marc Mutz <marc.mutz@kdab.com> | 2016-04-28 09:40:49 +0200 |
|---|---|---|
| committer | Marc Mutz <marc.mutz@kdab.com> | 2016-05-10 21:18:46 +0000 |
| commit | a5159cc50aa0f8a57b6f736621b359a3bcecbf7e (patch) | |
| tree | b563d6cc510c93e62f099e93922e7133c3959758 /src/corelib/json/qjsonobject.cpp | |
| parent | 5e51b15066eab5fd58f3975e5b7d47d9605ca725 (diff) | |
QJsonObject: add some overloads taking QLatin1String
QXmlStreamReader also has QLatin1String overloads, which
greatly benefits parsers, since the vast majority of keys
in both JSON and XML are US-ASCII. This patch adds such an
overload to the JSON parser.
The value() function is all typical parsers need, so even
though many more QJsonObject functions taking QString could
benefit from the same treatment, value() is the single most
important one for read-only JSON access.
Add some more overloads, too, for functions that don't need
more internal scaffolding than value(). Requires adding a
dummy op[](QL1S) (forwarding to the QString overload) so as
not to make
QJsonObject json;
json[QLatin1String("key")]; // mutable
ambiguous between const op[](QL1S) and mutable op[](QString).
[ChangeLog][QtCore][QJsonObject] Added value(), op[] const,
find(), constFind(), contains() overloads taking QLatin1String.
Change-Id: I00883028956ad949ba5ba2b18dd8a6a25ad5085b
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src/corelib/json/qjsonobject.cpp')
| -rw-r--r-- | src/corelib/json/qjsonobject.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index 7badc9d929f..b5b6f36bc6c 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -375,6 +375,22 @@ QJsonValue QJsonObject::value(const QString &key) const } /*! + \overload + \since 5.7 +*/ +QJsonValue QJsonObject::value(QLatin1String key) const +{ + if (!d) + return QJsonValue(QJsonValue::Undefined); + + bool keyExists; + int i = o->indexOf(key, &keyExists); + if (!keyExists) + return QJsonValue(QJsonValue::Undefined); + return QJsonValue(d, o, o->entryAt(i)->value); +} + +/*! Returns a QJsonValue representing the value for the key \a key. This does the same as value(). @@ -389,6 +405,13 @@ QJsonValue QJsonObject::operator [](const QString &key) const } /*! + \fn QJsonValue QJsonObject::operator [](QLatin1String key) const + + \overload + \since 5.7 +*/ + +/*! Returns a reference to the value for \a key. The return value is of type QJsonValueRef, a helper class for QJsonArray @@ -412,6 +435,16 @@ QJsonValueRef QJsonObject::operator [](const QString &key) } /*! + \overload + \since 5.7 +*/ +QJsonValueRef QJsonObject::operator [](QLatin1String key) +{ + // ### optimize me + return operator[](QString(key)); +} + +/*! Inserts a new item with the key \a key and a value of \a value. If there is already an item with the key \a key, then that item's value @@ -536,6 +569,20 @@ bool QJsonObject::contains(const QString &key) const } /*! + \overload + \since 5.7 +*/ +bool QJsonObject::contains(QLatin1String key) const +{ + if (!o) + return false; + + bool keyExists; + o->indexOf(key, &keyExists); + return keyExists; +} + +/*! Returns \c true if \a other is equal to this object. */ bool QJsonObject::operator==(const QJsonObject &other) const @@ -609,11 +656,31 @@ QJsonObject::iterator QJsonObject::find(const QString &key) return iterator(this, index); } +/*! + \overload + \since 5.7 +*/ +QJsonObject::iterator QJsonObject::find(QLatin1String key) +{ + bool keyExists = false; + int index = o ? o->indexOf(key, &keyExists) : 0; + if (!keyExists) + return end(); + detach2(); + return iterator(this, index); +} + /*! \fn QJsonObject::const_iterator QJsonObject::find(const QString &key) const \overload */ +/*! \fn QJsonObject::const_iterator QJsonObject::find(QLatin1String key) const + + \overload + \since 5.7 +*/ + /*! Returns a const iterator pointing to the item with key \a key in the map. @@ -630,6 +697,19 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const return const_iterator(this, index); } +/*! + \overload + \since 5.7 +*/ +QJsonObject::const_iterator QJsonObject::constFind(QLatin1String key) const +{ + bool keyExists = false; + int index = o ? o->indexOf(key, &keyExists) : 0; + if (!keyExists) + return end(); + return const_iterator(this, index); +} + /*! \fn int QJsonObject::count() const \overload |
