diff options
Diffstat (limited to 'src/corelib/serialization/qjsonobject.cpp')
-rw-r--r-- | src/corelib/serialization/qjsonobject.cpp | 392 |
1 files changed, 177 insertions, 215 deletions
diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index beeb3b5a54..2f61de0824 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <qjsonobject.h> #include <qjsonvalue.h> @@ -45,6 +9,8 @@ #include <qdebug.h> #include <qvariant.h> #include <qcbormap.h> +#include <qmap.h> +#include <qhash.h> #include <private/qcborvalue_p.h> #include "qjsonwriter_p.h" @@ -59,11 +25,16 @@ QT_BEGIN_NAMESPACE \inmodule QtCore \ingroup json \ingroup shared + \ingroup qtserialization \reentrant \since 5.0 \brief The QJsonObject class encapsulates a JSON object. + \compares equality + \compareswith equality QJsonValue QJsonValueConstRef + \endcompareswith + A JSON object is a list of key value pairs, where the keys are unique strings and the values are represented by a QJsonValue. @@ -76,7 +47,7 @@ QT_BEGIN_NAMESPACE You can convert the object to and from text based JSON through QJsonDocument. - \sa {JSON Support in Qt}, {JSON Save Game Example} + \sa {JSON Support in Qt}, {Saving and Loading a Game} */ /*! @@ -106,7 +77,7 @@ QT_BEGIN_NAMESPACE /*! \typedef QJsonObject::size_type - Typedef for int. Provided for STL compatibility. + Typedef for qsizetype. Provided for STL compatibility. */ @@ -118,7 +89,7 @@ QT_BEGIN_NAMESPACE QJsonObject::QJsonObject() = default; /*! - \fn QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) + \fn QJsonObject::QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args) \since 5.4 Constructs a QJsonObject instance initialized from \a args initialization list. For example: @@ -137,7 +108,6 @@ QJsonObject::QJsonObject() = default; QJsonObject::QJsonObject(QCborContainerPrivate *object) : o(object) { - Q_ASSERT(o); } /*! @@ -145,7 +115,7 @@ QJsonObject::QJsonObject(QCborContainerPrivate *object) */ QJsonObject::~QJsonObject() = default; -QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) +QJsonObject::QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args) { for (const auto &arg : args) insert(arg.first, arg.second); @@ -157,11 +127,13 @@ QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args Since QJsonObject is implicitly shared, the copy is shallow as long as the object does not get modified. */ -QJsonObject::QJsonObject(const QJsonObject &other) -{ - o = other.o; -} +QJsonObject::QJsonObject(const QJsonObject &other) noexcept = default; + +/*! + \since 5.10 + Move-constructs a QJsonObject from \a other. +*/ QJsonObject::QJsonObject(QJsonObject &&other) noexcept : o(other.o) { @@ -171,18 +143,8 @@ QJsonObject::QJsonObject(QJsonObject &&other) noexcept /*! Assigns \a other to this object. */ -QJsonObject &QJsonObject::operator =(const QJsonObject &other) -{ - o = other.o; - return *this; -} - -/*! - \fn QJsonObject::QJsonObject(QJsonObject &&other) - \since 5.10 +QJsonObject &QJsonObject::operator =(const QJsonObject &other) noexcept = default; - Move-constructs a QJsonObject from \a other. -*/ /*! \fn QJsonObject &QJsonObject::operator =(QJsonObject &&other) @@ -198,18 +160,21 @@ QJsonObject &QJsonObject::operator =(const QJsonObject &other) Swaps the object \a other with this. This operation is very fast and never fails. */ - +#ifndef QT_NO_VARIANT /*! Converts the variant map \a map to a QJsonObject. The keys in \a map will be used as the keys in the JSON object, and the QVariant values will be converted to JSON values. + \note Conversion from \l QVariant is not completely lossless. Please see + the documentation in QJsonValue::fromVariant() for more information. + \sa fromVariantHash(), toVariantMap(), QJsonValue::fromVariant() */ QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map) { - return QCborMap::fromVariantMap(map).toJsonObject(); + return QJsonPrivate::Variant::toJsonObject(map); } /*! @@ -231,6 +196,9 @@ QVariantMap QJsonObject::toVariantMap() const The keys in \a hash will be used as the keys in the JSON object, and the QVariant values will be converted to JSON values. + \note Conversion from \l QVariant is not completely lossless. Please see + the documentation in QJsonValue::fromVariant() for more information. + \sa fromVariantMap(), toVariantHash(), QJsonValue::fromVariant() */ QJsonObject QJsonObject::fromVariantHash(const QVariantHash &hash) @@ -255,18 +223,19 @@ QVariantHash QJsonObject::toVariantHash() const { return QCborMap::fromJsonObject(*this).toVariantHash(); } +#endif // !QT_NO_VARIANT /*! Returns a list of all keys in this object. - The list is sorted lexographically. + The list is sorted alphabetically. */ QStringList QJsonObject::keys() const { QStringList keys; if (o) { - keys.reserve(o->elements.length() / 2); - for (int i = 0, end = o->elements.length(); i < end; i += 2) + keys.reserve(o->elements.size() / 2); + for (qsizetype i = 0, end = o->elements.size(); i < end; i += 2) keys.append(o->stringAt(i)); } return keys; @@ -275,9 +244,9 @@ QStringList QJsonObject::keys() const /*! Returns the number of (key, value) pairs stored in the object. */ -int QJsonObject::size() const +qsizetype QJsonObject::size() const { - return o ? o->elements.length() / 2 : 0; + return o ? o->elements.size() / 2 : 0; } /*! @@ -291,8 +260,8 @@ bool QJsonObject::isEmpty() const } template<typename String> -static int indexOf(const QExplicitlySharedDataPointer<QCborContainerPrivate> &o, - String key, bool *keyExists) +static qsizetype indexOf(const QExplicitlySharedDataPointer<QCborContainerPrivate> &o, + String key, bool *keyExists) { const auto begin = QJsonPrivate::ConstKeyIterator(o->elements.constBegin()); const auto end = QJsonPrivate::ConstKeyIterator(o->elements.constEnd()); @@ -300,14 +269,13 @@ static int indexOf(const QExplicitlySharedDataPointer<QCborContainerPrivate> &o, const auto it = std::lower_bound( begin, end, key, [&](const QJsonPrivate::ConstKeyIterator::value_type &e, const String &key) { - return o->stringCompareElement(e.key(), key) < 0; + return o->stringCompareElement(e.key(), key, QtCbor::Comparison::ForOrdering) < 0; }); *keyExists = (it != end) && o->stringEqualsElement((*it).key(), key); - return (it - begin) * 2; + return it.it - begin.it; } -#if QT_STRINGVIEW_LEVEL < 2 /*! Returns a QJsonValue representing the value for the key \a key. @@ -319,7 +287,6 @@ QJsonValue QJsonObject::value(const QString &key) const { return value(QStringView(key)); } -#endif /*! \overload @@ -334,7 +301,7 @@ QJsonValue QJsonObject::value(QStringView key) const \overload \since 5.7 */ -QJsonValue QJsonObject::value(QLatin1String key) const +QJsonValue QJsonObject::value(QLatin1StringView key) const { return valueImpl(key); } @@ -349,13 +316,12 @@ QJsonValue QJsonObject::valueImpl(T key) const return QJsonValue(QJsonValue::Undefined); bool keyExists; - int i = indexOf(o, key, &keyExists); + auto i = indexOf(o, key, &keyExists); if (!keyExists) return QJsonValue(QJsonValue::Undefined); return QJsonPrivate::Value::fromTrustedCbor(o->valueAt(i + 1)); } -#if QT_STRINGVIEW_LEVEL < 2 /*! Returns a QJsonValue representing the value for the key \a key. @@ -369,7 +335,6 @@ QJsonValue QJsonObject::operator [](const QString &key) const { return (*this)[QStringView(key)]; } -#endif /*! \fn QJsonValue QJsonObject::operator [](QStringView key) const @@ -379,13 +344,12 @@ QJsonValue QJsonObject::operator [](const QString &key) const */ /*! - \fn QJsonValue QJsonObject::operator [](QLatin1String key) const + \fn QJsonValue QJsonObject::operator [](QLatin1StringView key) const \overload \since 5.7 */ -#if QT_STRINGVIEW_LEVEL < 2 /*! Returns a reference to the value for \a key. If there is no value with key \a key in the object, one is created with a QJsonValue::Null value and then @@ -403,7 +367,6 @@ QJsonValueRef QJsonObject::operator [](const QString &key) { return (*this)[QStringView(key)]; } -#endif /*! \overload @@ -418,7 +381,7 @@ QJsonValueRef QJsonObject::operator [](QStringView key) \overload \since 5.7 */ -QJsonValueRef QJsonObject::operator [](QLatin1String key) +QJsonValueRef QJsonObject::operator [](QLatin1StringView key) { return atImpl(key); } @@ -433,9 +396,9 @@ QJsonValueRef QJsonObject::atImpl(T key) o = new QCborContainerPrivate; bool keyExists = false; - int index = indexOf(o, key, &keyExists); + auto index = indexOf(o, key, &keyExists); if (!keyExists) { - detach(o->elements.length() / 2 + 1); + detach(o->elements.size() / 2 + 1); o->insertAt(index, key); o->insertAt(index + 1, QCborValue::fromJsonValue(QJsonValue())); } @@ -443,7 +406,6 @@ QJsonValueRef QJsonObject::atImpl(T key) return QJsonValueRef(this, index / 2); } -#if QT_STRINGVIEW_LEVEL < 2 /*! Inserts a new item with the key \a key and a value of \a value. @@ -461,7 +423,6 @@ QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue & { return insert(QStringView(key), value); } -#endif /*! \overload @@ -476,7 +437,7 @@ QJsonObject::iterator QJsonObject::insert(QStringView key, const QJsonValue &val \overload \since 5.14 */ -QJsonObject::iterator QJsonObject::insert(QLatin1String key, const QJsonValue &value) +QJsonObject::iterator QJsonObject::insert(QLatin1StringView key, const QJsonValue &value) { return insertImpl(key, value); } @@ -492,7 +453,7 @@ QJsonObject::iterator QJsonObject::insertImpl(T key, const QJsonValue &value) return end(); } bool keyExists = false; - int pos = o ? indexOf(o, key, &keyExists) : 0; + auto pos = o ? indexOf(o, key, &keyExists) : 0; return insertAt(pos, key, value, keyExists); } @@ -500,10 +461,10 @@ QJsonObject::iterator QJsonObject::insertImpl(T key, const QJsonValue &value) \internal */ template <typename T> -QJsonObject::iterator QJsonObject::insertAt(int pos, T key, const QJsonValue &value, bool keyExists) +QJsonObject::iterator QJsonObject::insertAt(qsizetype pos, T key, const QJsonValue &value, bool keyExists) { if (o) - detach(o->elements.length() / 2 + (keyExists ? 0 : 1)); + detach(o->elements.size() / 2 + (keyExists ? 0 : 1)); else o = new QCborContainerPrivate; @@ -516,7 +477,6 @@ QJsonObject::iterator QJsonObject::insertAt(int pos, T key, const QJsonValue &va return {this, pos / 2}; } -#if QT_STRINGVIEW_LEVEL < 2 /*! Removes \a key from the object. @@ -526,7 +486,6 @@ void QJsonObject::remove(const QString &key) { remove(QStringView(key)); } -#endif /*! \overload @@ -541,7 +500,7 @@ void QJsonObject::remove(QStringView key) \overload \since 5.14 */ -void QJsonObject::remove(QLatin1String key) +void QJsonObject::remove(QLatin1StringView key) { removeImpl(key); } @@ -556,14 +515,13 @@ void QJsonObject::removeImpl(T key) return; bool keyExists; - int index = indexOf(o, key, &keyExists); + auto index = indexOf(o, key, &keyExists); if (!keyExists) return; - removeAt(index / 2); + removeAt(index); } -#if QT_STRINGVIEW_LEVEL < 2 /*! Removes \a key from the object. @@ -577,7 +535,6 @@ QJsonValue QJsonObject::take(const QString &key) { return take(QStringView(key)); } -#endif /*! \overload @@ -592,7 +549,7 @@ QJsonValue QJsonObject::take(QStringView key) \overload \since 5.14 */ -QJsonValue QJsonObject::take(QLatin1String key) +QJsonValue QJsonObject::take(QLatin1StringView key) { return takeImpl(key); } @@ -607,16 +564,16 @@ QJsonValue QJsonObject::takeImpl(T key) return QJsonValue(QJsonValue::Undefined); bool keyExists; - int index = indexOf(o, key, &keyExists); + auto index = indexOf(o, key, &keyExists); if (!keyExists) return QJsonValue(QJsonValue::Undefined); + detach(); const QJsonValue v = QJsonPrivate::Value::fromTrustedCbor(o->extractAt(index + 1)); - removeAt(index / 2); + removeAt(index); return v; } -#if QT_STRINGVIEW_LEVEL < 2 /*! Returns \c true if the object contains key \a key. @@ -626,7 +583,6 @@ bool QJsonObject::contains(const QString &key) const { return contains(QStringView(key)); } -#endif /*! \overload @@ -641,7 +597,7 @@ bool QJsonObject::contains(QStringView key) const \overload \since 5.7 */ -bool QJsonObject::contains(QLatin1String key) const +bool QJsonObject::contains(QLatin1StringView key) const { return containsImpl(key); } @@ -661,22 +617,24 @@ bool QJsonObject::containsImpl(T key) const } /*! - Returns \c true if \a other is equal to this object. - */ -bool QJsonObject::operator==(const QJsonObject &other) const + \fn bool QJsonObject::operator==(const QJsonObject &lhs, const QJsonObject &rhs) + + Returns \c true if \a lhs object is equal to \a rhs, \c false otherwise. +*/ +bool comparesEqual(const QJsonObject &lhs, const QJsonObject &rhs) noexcept { - if (o == other.o) + if (lhs.o == rhs.o) return true; - if (!o) - return !other.o->elements.length(); - if (!other.o) - return !o->elements.length(); - if (o->elements.length() != other.o->elements.length()) + if (!lhs.o) + return !rhs.o->elements.size(); + if (!rhs.o) + return !lhs.o->elements.size(); + if (lhs.o->elements.size() != rhs.o->elements.size()) return false; - for (int i = 0, end = o->elements.length(); i < end; ++i) { - if (o->valueAt(i) != other.o->valueAt(i)) + for (qsizetype i = 0, end = lhs.o->elements.size(); i < end; ++i) { + if (lhs.o->valueAt(i) != rhs.o->valueAt(i)) return false; } @@ -684,12 +642,10 @@ bool QJsonObject::operator==(const QJsonObject &other) const } /*! - Returns \c true if \a other is not equal to this object. - */ -bool QJsonObject::operator!=(const QJsonObject &other) const -{ - return !(*this == other); -} + \fn bool QJsonObject::operator!=(const QJsonObject &lhs, const QJsonObject &rhs) + + Returns \c true if \a lhs object is not equal to \a rhs, \c false otherwise. +*/ /*! Removes the (key, value) pair pointed to by the iterator \a it @@ -700,16 +656,14 @@ bool QJsonObject::operator!=(const QJsonObject &other) const */ QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it) { - if (it.item.o != this || it.item.index < 0 || it.item.index >= o->elements.length()) - return {this, int(o->elements.length())}; - - removeAt(it.item.index); + removeAt(it.item.index * 2); - // iterator hasn't changed - return it; + // index hasn't changed; the container pointer shouldn't have changed + // because we shouldn't have detached (detaching happens on obtaining a + // non-const iterator). But just in case we did, reload the pointer. + return { this, qsizetype(it.item.index) }; } -#if QT_STRINGVIEW_LEVEL < 2 /*! Returns an iterator pointing to the item with key \a key in the map. @@ -721,7 +675,6 @@ QJsonObject::iterator QJsonObject::find(const QString &key) { return find(QStringView(key)); } -#endif /*! \overload @@ -736,7 +689,7 @@ QJsonObject::iterator QJsonObject::find(QStringView key) \overload \since 5.7 */ -QJsonObject::iterator QJsonObject::find(QLatin1String key) +QJsonObject::iterator QJsonObject::find(QLatin1StringView key) { return findImpl(key); } @@ -748,19 +701,17 @@ template <typename T> QJsonObject::iterator QJsonObject::findImpl(T key) { bool keyExists = false; - int index = o ? indexOf(o, key, &keyExists) : 0; + auto index = o ? indexOf(o, key, &keyExists) : 0; if (!keyExists) return end(); detach(); return {this, index / 2}; } -#if QT_STRINGVIEW_LEVEL < 2 /*! \fn QJsonObject::const_iterator QJsonObject::find(const QString &key) const \overload */ -#endif /*! \fn QJsonObject::const_iterator QJsonObject::find(QStringView key) const @@ -768,13 +719,12 @@ QJsonObject::iterator QJsonObject::findImpl(T key) \since 5.14 */ -/*! \fn QJsonObject::const_iterator QJsonObject::find(QLatin1String key) const +/*! \fn QJsonObject::const_iterator QJsonObject::find(QLatin1StringView key) const \overload \since 5.7 */ -#if QT_STRINGVIEW_LEVEL < 2 /*! Returns a const iterator pointing to the item with key \a key in the map. @@ -786,7 +736,6 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const { return constFind(QStringView(key)); } -#endif /*! \overload @@ -801,7 +750,7 @@ QJsonObject::const_iterator QJsonObject::constFind(QStringView key) const \overload \since 5.7 */ -QJsonObject::const_iterator QJsonObject::constFind(QLatin1String key) const +QJsonObject::const_iterator QJsonObject::constFind(QLatin1StringView key) const { return constFindImpl(key); } @@ -813,20 +762,20 @@ template <typename T> QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const { bool keyExists = false; - int index = o ? indexOf(o, key, &keyExists) : 0; + auto index = o ? indexOf(o, key, &keyExists) : 0; if (!keyExists) return end(); return {this, index / 2}; } -/*! \fn int QJsonObject::count() const +/*! \fn qsizetype QJsonObject::count() const \overload Same as size(). */ -/*! \fn int QJsonObject::length() const +/*! \fn qsizetype QJsonObject::length() const \overload @@ -891,6 +840,10 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \brief The QJsonObject::iterator class provides an STL-style non-const iterator for QJsonObject. + \compares strong + \compareswith strong QJsonObject::const_iterator + \endcompareswith + QJsonObject::iterator allows you to iterate over a QJsonObject and to modify the value (but not the key) stored under a particular key. If you want to iterate over a const QJsonObject, you @@ -907,7 +860,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const Multiple iterators can be used on the same object. Existing iterators will however become dangling once the object gets modified. - \sa QJsonObject::const_iterator, {JSON Support in Qt}, {JSON Save Game Example} + \sa QJsonObject::const_iterator, {JSON Support in Qt}, {Saving and Loading a Game} */ /*! \typedef QJsonObject::iterator::difference_type @@ -950,7 +903,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa QJsonObject::begin(), QJsonObject::end() */ -/*! \fn QJsonObject::iterator::iterator(QJsonObject *obj, int index) +/*! \fn QJsonObject::iterator::iterator(QJsonObject *obj, qsizetype index) \internal */ @@ -996,12 +949,17 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa key() */ -/*! \fn QJsonValueRef *QJsonObject::iterator::operator->() const +/*! \fn QJsonValueRef *QJsonObject::iterator::operator->() Returns a pointer to a modifiable reference to the current item. */ -/*! \fn const QJsonValueRef QJsonObject::iterator::operator[](int j) +/*! \fn const QJsonValueConstRef *QJsonObject::iterator::operator->() const + + Returns a pointer to a constant reference to the current item. +*/ + +/*! \fn const QJsonValueRef QJsonObject::iterator::operator[](qsizetype j) const Returns a modifiable reference to the item at offset \a j from the item pointed to by this iterator (the item at position \c{*this + j}). @@ -1019,60 +977,60 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const */ /*! - \fn bool QJsonObject::iterator::operator==(const iterator &other) const - \fn bool QJsonObject::iterator::operator==(const const_iterator &other) const + \fn bool QJsonObject::iterator::operator==(const iterator &lhs, const iterator &rhs) + \fn bool QJsonObject::iterator::operator==(const iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to the same item as this + Returns \c true if \a lhs points to the same item as \a rhs iterator; otherwise returns \c false. \sa operator!=() */ /*! - \fn bool QJsonObject::iterator::operator!=(const iterator &other) const - \fn bool QJsonObject::iterator::operator!=(const const_iterator &other) const + \fn bool QJsonObject::iterator::operator!=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonObject::iterator::operator!=(const iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to a different item than this + Returns \c true if \a lhs points to a different item than \a rhs iterator; otherwise returns \c false. \sa operator==() */ /*! - \fn bool QJsonObject::iterator::operator<(const iterator& other) const - \fn bool QJsonObject::iterator::operator<(const const_iterator& other) const + \fn bool QJsonObject::iterator::operator<(const iterator &lhs, const iterator &rhs) + \fn bool QJsonObject::iterator::operator<(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonObject::iterator::operator<=(const iterator& other) const - \fn bool QJsonObject::iterator::operator<=(const const_iterator& other) const + \fn bool QJsonObject::iterator::operator<=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonObject::iterator::operator<=(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + or equal to the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonObject::iterator::operator>(const iterator& other) const - \fn bool QJsonObject::iterator::operator>(const const_iterator& other) const + \fn bool QJsonObject::iterator::operator>(const iterator &lhs, const iterator &rhs) + \fn bool QJsonObject::iterator::operator>(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonObject::iterator::operator>=(const iterator& other) const - \fn bool QJsonObject::iterator::operator>=(const const_iterator& other) const + \fn bool QJsonObject::iterator::operator>=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonObject::iterator::operator>=(const iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than or equal to the item pointed to by the \a rhs iterator. */ /*! \fn QJsonObject::iterator QJsonObject::iterator::operator++() - The prefix ++ operator, \c{++i}, advances the iterator to the + The prefix \c{++} operator, \c{++i}, advances the iterator to the next item in the object and returns an iterator to the new current item. @@ -1085,14 +1043,14 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \overload - The postfix ++ operator, \c{i++}, advances the iterator to the + The postfix \c{++} operator, \c{i++}, advances the iterator to the next item in the object and returns an iterator to the previously current item. */ /*! \fn QJsonObject::iterator QJsonObject::iterator::operator--() - The prefix -- operator, \c{--i}, makes the preceding item + The prefix \c{--} operator, \c{--i}, makes the preceding item current and returns an iterator pointing to the new current item. Calling this function on QJsonObject::begin() leads to undefined @@ -1105,12 +1063,12 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \overload - The postfix -- operator, \c{i--}, makes the preceding item + The postfix \c{--} operator, \c{i--}, makes the preceding item current and returns an iterator pointing to the previously current item. */ -/*! \fn QJsonObject::iterator QJsonObject::iterator::operator+(int j) const +/*! \fn QJsonObject::iterator QJsonObject::iterator::operator+(qsizetype j) const Returns an iterator to the item at \a j positions forward from this iterator. If \a j is negative, the iterator goes backward. @@ -1119,7 +1077,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const */ -/*! \fn QJsonObject::iterator QJsonObject::iterator::operator-(int j) const +/*! \fn QJsonObject::iterator QJsonObject::iterator::operator-(qsizetype j) const Returns an iterator to the item at \a j positions backward from this iterator. If \a j is negative, the iterator goes forward. @@ -1127,7 +1085,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa operator+() */ -/*! \fn QJsonObject::iterator &QJsonObject::iterator::operator+=(int j) +/*! \fn QJsonObject::iterator &QJsonObject::iterator::operator+=(qsizetype j) Advances the iterator by \a j items. If \a j is negative, the iterator goes backward. @@ -1135,7 +1093,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa operator-=(), operator+() */ -/*! \fn QJsonObject::iterator &QJsonObject::iterator::operator-=(int j) +/*! \fn QJsonObject::iterator &QJsonObject::iterator::operator-=(qsizetype j) Makes the iterator go back by \a j items. If \a j is negative, the iterator goes forward. @@ -1143,7 +1101,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa operator+=(), operator-() */ -/*! \fn int QJsonObject::iterator::operator-(iterator other) const +/*! \fn qsizetype QJsonObject::iterator::operator-(iterator other) const Returns the number of items between the item pointed to by \a other and the item pointed to by this iterator. @@ -1156,6 +1114,10 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \since 5.0 \brief The QJsonObject::const_iterator class provides an STL-style const iterator for QJsonObject. + \compares strong + \compareswith strong QJsonObject::iterator + \endcompareswith + QJsonObject::const_iterator allows you to iterate over a QJsonObject. If you want to modify the QJsonObject as you iterate over it, you must use QJsonObject::iterator instead. It is generally @@ -1172,7 +1134,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const Multiple iterators can be used on the same object. Existing iterators will however become dangling if the object gets modified. - \sa QJsonObject::iterator, {JSON Support in Qt}, {JSON Save Game Example} + \sa QJsonObject::iterator, {JSON Support in Qt}, {Saving and Loading a Game} */ /*! \typedef QJsonObject::const_iterator::difference_type @@ -1215,7 +1177,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa QJsonObject::constBegin(), QJsonObject::constEnd() */ -/*! \fn QJsonObject::const_iterator::const_iterator(const QJsonObject *obj, int index) +/*! \fn QJsonObject::const_iterator::const_iterator(const QJsonObject *obj, qsizetype index) \internal */ @@ -1231,14 +1193,14 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa value() */ -/*! \fn QJsonValueRef QJsonObject::const_iterator::value() const +/*! \fn QJsonValueConstRef QJsonObject::const_iterator::value() const Returns the current item's value. \sa key(), operator*() */ -/*! \fn const QJsonValueRef QJsonObject::const_iterator::operator*() const +/*! \fn const QJsonValueConstRef QJsonObject::const_iterator::operator*() const Returns the current item's value. @@ -1247,12 +1209,12 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa key() */ -/*! \fn const QJsonValueRef *QJsonObject::const_iterator::operator->() const +/*! \fn const QJsonValueConstRef *QJsonObject::const_iterator::operator->() const Returns a pointer to the current item. */ -/*! \fn const QJsonValue QJsonObject::const_iterator::operator[](int j) +/*! \fn const QJsonValueConstRef QJsonObject::const_iterator::operator[](qsizetype j) const Returns the item at offset \a j from the item pointed to by this iterator (the item at position \c{*this + j}). @@ -1264,55 +1226,53 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const */ -/*! \fn bool QJsonObject::const_iterator::operator==(const const_iterator &other) const - \fn bool QJsonObject::const_iterator::operator==(const iterator &other) const +/*! \fn bool QJsonObject::const_iterator::operator==(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to the same item as this + Returns \c true if \a lhs points to the same item as \a rhs iterator; otherwise returns \c false. \sa operator!=() */ -/*! \fn bool QJsonObject::const_iterator::operator!=(const const_iterator &other) const - \fn bool QJsonObject::const_iterator::operator!=(const iterator &other) const +/*! \fn bool QJsonObject::const_iterator::operator!=(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if \a other points to a different item than this + Returns \c true if \a lhs points to a different item than \a rhs iterator; otherwise returns \c false. \sa operator==() */ /*! - \fn bool QJsonObject::const_iterator::operator<(const const_iterator& other) const + \fn bool QJsonObject::const_iterator::operator<(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonObject::const_iterator::operator<=(const const_iterator& other) const + \fn bool QJsonObject::const_iterator::operator<=(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is less than - or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is less than + or equal to the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonObject::const_iterator::operator>(const const_iterator& other) const + \fn bool QJsonObject::const_iterator::operator>(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than the item pointed to by the \a rhs iterator. */ /*! - \fn bool QJsonObject::const_iterator::operator>=(const const_iterator& other) const + \fn bool QJsonObject::const_iterator::operator>=(const const_iterator &lhs, const const_iterator &rhs) - Returns \c true if the item pointed to by this iterator is greater - than or equal to the item pointed to by the \a other iterator. + Returns \c true if the item pointed to by \a lhs iterator is greater + than or equal to the item pointed to by the \a rhs iterator. */ /*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator++() - The prefix ++ operator, \c{++i}, advances the iterator to the + The prefix \c{++} operator, \c{++i}, advances the iterator to the next item in the object and returns an iterator to the new current item. @@ -1325,14 +1285,14 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \overload - The postfix ++ operator, \c{i++}, advances the iterator to the + The postfix \c{++} operator, \c{i++}, advances the iterator to the next item in the object and returns an iterator to the previously current item. */ /*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator--() - The prefix -- operator, \c{--i}, makes the preceding item + The prefix \c{--} operator, \c{--i}, makes the preceding item current and returns an iterator pointing to the new current item. Calling this function on QJsonObject::begin() leads to undefined @@ -1345,12 +1305,12 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \overload - The postfix -- operator, \c{i--}, makes the preceding item + The postfix \c{--} operator, \c{i--}, makes the preceding item current and returns an iterator pointing to the previously current item. */ -/*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator+(int j) const +/*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator+(qsizetype j) const Returns an iterator to the item at \a j positions forward from this iterator. If \a j is negative, the iterator goes backward. @@ -1360,7 +1320,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa operator-() */ -/*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator-(int j) const +/*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator-(qsizetype j) const Returns an iterator to the item at \a j positions backward from this iterator. If \a j is negative, the iterator goes forward. @@ -1370,7 +1330,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa operator+() */ -/*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator+=(int j) +/*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator+=(qsizetype j) Advances the iterator by \a j items. If \a j is negative, the iterator goes backward. @@ -1380,7 +1340,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa operator-=(), operator+() */ -/*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator-=(int j) +/*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator-=(qsizetype j) Makes the iterator go back by \a j items. If \a j is negative, the iterator goes forward. @@ -1390,7 +1350,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const \sa operator+=(), operator-() */ -/*! \fn int QJsonObject::const_iterator::operator-(const_iterator other) const +/*! \fn qsizetype QJsonObject::const_iterator::operator-(const_iterator other) const Returns the number of items between the item pointed to by \a other and the item pointed to by this iterator. @@ -1400,29 +1360,30 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const /*! \internal */ -bool QJsonObject::detach(uint reserve) +bool QJsonObject::detach(qsizetype reserve) { if (!o) return true; - o = QCborContainerPrivate::detach(o.data(), reserve ? reserve * 2 : o->elements.length()); + o = QCborContainerPrivate::detach(o.data(), reserve ? reserve * 2 : o->elements.size()); return o; } +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) /*! \internal */ -QString QJsonObject::keyAt(int i) const +QString QJsonObject::keyAt(qsizetype i) const { - Q_ASSERT(o && i >= 0 && i * 2 < o->elements.length()); + Q_ASSERT(o && i >= 0 && i * 2 < o->elements.size()); return o->stringAt(i * 2); } /*! \internal */ -QJsonValue QJsonObject::valueAt(int i) const +QJsonValue QJsonObject::valueAt(qsizetype i) const { - if (!o || i < 0 || 2 * i + 1 >= o->elements.length()) + if (!o || i < 0 || 2 * i + 1 >= o->elements.size()) return QJsonValue(QJsonValue::Undefined); return QJsonPrivate::Value::fromTrustedCbor(o->valueAt(2 * i + 1)); } @@ -1430,9 +1391,9 @@ QJsonValue QJsonObject::valueAt(int i) const /*! \internal */ -void QJsonObject::setValueAt(int i, const QJsonValue &val) +void QJsonObject::setValueAt(qsizetype i, const QJsonValue &val) { - Q_ASSERT(o && i >= 0 && 2 * i + 1 < o->elements.length()); + Q_ASSERT(o && i >= 0 && 2 * i + 1 < o->elements.size()); detach(); if (val.isUndefined()) { o->removeAt(2 * i + 1); @@ -1441,15 +1402,16 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val) o->replaceAt(2 * i + 1, QCborValue::fromJsonValue(val)); } } +#endif // Qt 7 /*! \internal */ -void QJsonObject::removeAt(int index) +void QJsonObject::removeAt(qsizetype index) { detach(); - o->removeAt(2 * index + 1); - o->removeAt(2 * index); + o->removeAt(index + 1); + o->removeAt(index); } size_t qHash(const QJsonObject &object, size_t seed) |