diff options
Diffstat (limited to 'src/corelib/json')
-rw-r--r-- | src/corelib/json/qjson.cpp | 32 | ||||
-rw-r--r-- | src/corelib/json/qjson_p.h | 20 | ||||
-rw-r--r-- | src/corelib/json/qjsonobject.cpp | 81 | ||||
-rw-r--r-- | src/corelib/json/qjsonobject.h | 7 | ||||
-rw-r--r-- | src/corelib/json/qjsonvalue.cpp | 16 | ||||
-rw-r--r-- | src/corelib/json/qjsonvalue.h | 3 |
6 files changed, 156 insertions, 3 deletions
diff --git a/src/corelib/json/qjson.cpp b/src/corelib/json/qjson.cpp index 24fcaf0937..bb98e25fa5 100644 --- a/src/corelib/json/qjson.cpp +++ b/src/corelib/json/qjson.cpp @@ -179,7 +179,29 @@ void Base::removeItems(int pos, int numItems) length -= numItems; } -int Object::indexOf(const QString &key, bool *exists) +int Object::indexOf(const QString &key, bool *exists) const +{ + int min = 0; + int n = length; + while (n > 0) { + int half = n >> 1; + int middle = min + half; + if (*entryAt(middle) >= key) { + n = half; + } else { + min = middle + 1; + n -= half + 1; + } + } + if (min < (int)length && *entryAt(min) == key) { + *exists = true; + return min; + } + *exists = false; + return min; +} + +int Object::indexOf(QLatin1String key, bool *exists) const { int min = 0; int n = length; @@ -248,6 +270,14 @@ bool Entry::operator ==(const QString &key) const return (shallowKey() == key); } +bool Entry::operator==(QLatin1String key) const +{ + if (value.latinKey) + return shallowLatin1Key() == key; + else + return shallowKey() == key; +} + bool Entry::operator ==(const Entry &other) const { if (value.latinKey) { diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index e5010c2da7..5e34845fe3 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -122,6 +122,7 @@ QT_BEGIN_NAMESPACE Other measurements have shown a slightly bigger binary size than a compact text representation where all possible whitespace was stripped out. */ +#define Q_DECLARE_JSONPRIVATE_TYPEINFO(Class, Flags) } Q_DECLARE_TYPEINFO(QJsonPrivate::Class, Flags); namespace QJsonPrivate { namespace QJsonPrivate { class Array; @@ -572,7 +573,8 @@ public: Entry *entryAt(int i) const { return reinterpret_cast<Entry *>(((char *)this) + table()[i]); } - int indexOf(const QString &key, bool *exists); + int indexOf(const QString &key, bool *exists) const; + int indexOf(QLatin1String key, bool *exists) const; bool isValid() const; }; @@ -619,6 +621,7 @@ public: static uint valueToStore(const QJsonValue &v, uint offset); static void copyData(const QJsonValue &v, char *dest, bool compressed); }; +Q_DECLARE_JSONPRIVATE_TYPEINFO(Value, Q_PRIMITIVE_TYPE) inline Value Array::at(int i) const { @@ -673,6 +676,10 @@ public: inline bool operator !=(const QString &key) const { return !operator ==(key); } inline bool operator >=(const QString &key) const; + bool operator==(QLatin1String key) const; + inline bool operator!=(QLatin1String key) const { return !operator ==(key); } + inline bool operator>=(QLatin1String key) const; + bool operator ==(const Entry &other) const; bool operator >=(const Entry &other) const; }; @@ -685,9 +692,20 @@ inline bool Entry::operator >=(const QString &key) const return (shallowKey() >= key); } +inline bool Entry::operator >=(QLatin1String key) const +{ + if (value.latinKey) + return shallowLatin1Key() >= key; + else + return shallowKey() >= key; +} + inline bool operator <(const QString &key, const Entry &e) { return e >= key; } +inline bool operator<(QLatin1String key, const Entry &e) +{ return e >= key; } + class Header { public: diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index 4ee20ef168..b5b6f36bc6 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -304,6 +304,7 @@ QVariantHash QJsonObject::toVariantHash() const { QVariantHash hash; if (o) { + hash.reserve(o->length); for (uint i = 0; i < o->length; ++i) { QJsonPrivate::Entry *e = o->entryAt(i); hash.insert(e->key(), QJsonValue(d, o, e->value).toVariant()); @@ -374,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(). @@ -388,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 @@ -411,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 @@ -535,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 @@ -608,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. @@ -629,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 diff --git a/src/corelib/json/qjsonobject.h b/src/corelib/json/qjsonobject.h index 6cffbce83a..e238c84d98 100644 --- a/src/corelib/json/qjsonobject.h +++ b/src/corelib/json/qjsonobject.h @@ -86,12 +86,16 @@ public: bool isEmpty() const; QJsonValue value(const QString &key) const; + QJsonValue value(QLatin1String key) const; QJsonValue operator[] (const QString &key) const; + QJsonValue operator[] (QLatin1String key) const { return value(key); } QJsonValueRef operator[] (const QString &key); + QJsonValueRef operator[] (QLatin1String key); void remove(const QString &key); QJsonValue take(const QString &key); bool contains(const QString &key) const; + bool contains(QLatin1String key) const; bool operator==(const QJsonObject &other) const; bool operator!=(const QJsonObject &other) const; @@ -200,8 +204,11 @@ public: typedef iterator Iterator; typedef const_iterator ConstIterator; iterator find(const QString &key); + iterator find(QLatin1String key); const_iterator find(const QString &key) const { return constFind(key); } + const_iterator find(QLatin1String key) const { return constFind(key); } const_iterator constFind(const QString &key) const; + const_iterator constFind(QLatin1String key) const; iterator insert(const QString &key, const QJsonValue &value); // STL compatibility diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp index 36df146332..b21f59ae35 100644 --- a/src/corelib/json/qjsonvalue.cpp +++ b/src/corelib/json/qjsonvalue.cpp @@ -564,6 +564,22 @@ QString QJsonValue::toString(const QString &defaultValue) const } /*! + Converts the value to a QString and returns it. + + If type() is not String, a null QString will be returned. + + \sa QString::isNull() + */ +QString QJsonValue::toString() const +{ + if (t != String) + return QString(); + stringData->ref.ref(); // the constructor below doesn't add a ref. + QStringDataPtr holder = { stringData }; + return QString(holder); +} + +/*! Converts the value to an array and returns it. If type() is not Array, the \a defaultValue will be returned. diff --git a/src/corelib/json/qjsonvalue.h b/src/corelib/json/qjsonvalue.h index 8b6221ea4c..1ce7f745e0 100644 --- a/src/corelib/json/qjsonvalue.h +++ b/src/corelib/json/qjsonvalue.h @@ -107,7 +107,8 @@ public: 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; + QString toString() const; + QString toString(const QString &defaultValue) const; QJsonArray toArray() const; QJsonArray toArray(const QJsonArray &defaultValue) const; QJsonObject toObject() const; |