diff options
Diffstat (limited to 'src/corelib/serialization/qjsonarray.cpp')
-rw-r--r-- | src/corelib/serialization/qjsonarray.cpp | 187 |
1 files changed, 97 insertions, 90 deletions
diff --git a/src/corelib/serialization/qjsonarray.cpp b/src/corelib/serialization/qjsonarray.cpp index 94277db922..0c1b0ac7c8 100644 --- a/src/corelib/serialization/qjsonarray.cpp +++ b/src/corelib/serialization/qjsonarray.cpp @@ -22,11 +22,16 @@ QT_BEGIN_NAMESPACE \inmodule QtCore \ingroup json \ingroup shared + \ingroup qtserialization \reentrant \since 5.0 \brief The QJsonArray class encapsulates a JSON array. + \compares equality + \compareswith equality QJsonValue + \endcompareswith + A JSON array is a list of values. The list can be manipulated by inserting and removing QJsonValue's from the array. @@ -39,7 +44,7 @@ QT_BEGIN_NAMESPACE You can convert the array 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} */ /*! @@ -119,7 +124,6 @@ QJsonArray::QJsonArray() = default; QJsonArray::QJsonArray(QCborContainerPrivate *array) : a(array) { - Q_ASSERT(array); } /*! @@ -139,11 +143,13 @@ QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args) Since QJsonArray is implicitly shared, the copy is shallow as long as the object doesn't get modified. */ -QJsonArray::QJsonArray(const QJsonArray &other) -{ - a = other.a; -} +QJsonArray::QJsonArray(const QJsonArray &other) noexcept = default; + +/*! + \since 5.10 + Move-constructs a QJsonArray from \a other. +*/ QJsonArray::QJsonArray(QJsonArray &&other) noexcept : a(other.a) { @@ -153,18 +159,7 @@ QJsonArray::QJsonArray(QJsonArray &&other) noexcept /*! Assigns \a other to this array. */ -QJsonArray &QJsonArray::operator =(const QJsonArray &other) -{ - a = other.a; - return *this; -} - -/*! - \fn QJsonArray::QJsonArray(QJsonArray &&other) - \since 5.10 - - Move-constructs a QJsonArray from \a other. -*/ +QJsonArray &QJsonArray::operator =(const QJsonArray &other) noexcept = default; /*! \fn QJsonArray &QJsonArray::operator =(QJsonArray &&other) @@ -220,6 +215,7 @@ QJsonArray QJsonArray::fromStringList(const QStringList &list) return array; } +#ifndef QT_NO_VARIANT /*! Converts the variant list \a list to a QJsonArray. @@ -244,6 +240,7 @@ QVariantList QJsonArray::toVariantList() const { return QCborArray::fromJsonArray(*this).toVariantList(); } +#endif // !QT_NO_VARIANT /*! @@ -340,7 +337,7 @@ void QJsonArray::append(const QJsonValue &value) */ void QJsonArray::removeAt(qsizetype i) { - if (!a || i < 0 || i >= a->elements.length()) + if (!a || i < 0 || i >= a->elements.size()) return; detach(); a->removeAt(i); @@ -376,7 +373,7 @@ void QJsonArray::removeAt(qsizetype i) */ QJsonValue QJsonArray::takeAt(qsizetype i) { - if (!a || i < 0 || i >= a->elements.length()) + if (!a || i < 0 || i >= a->elements.size()) return QJsonValue(QJsonValue::Undefined); detach(); @@ -395,11 +392,11 @@ QJsonValue QJsonArray::takeAt(qsizetype i) void QJsonArray::insert(qsizetype i, const QJsonValue &value) { if (a) - detach(a->elements.length() + 1); + detach(a->elements.size() + 1); else a = new QCborContainerPrivate; - Q_ASSERT (i >= 0 && i <= a->elements.length()); + Q_ASSERT (i >= 0 && i <= a->elements.size()); a->insertAt(i, value.type() == QJsonValue::Undefined ? QCborValue(nullptr) : QCborValue::fromJsonValue(value)); } @@ -430,7 +427,7 @@ void QJsonArray::insert(qsizetype i, const QJsonValue &value) */ void QJsonArray::replace(qsizetype i, const QJsonValue &value) { - Q_ASSERT (a && i >= 0 && i < a->elements.length()); + Q_ASSERT (a && i >= 0 && i < a->elements.size()); detach(); a->replaceAt(i, QCborValue::fromJsonValue(value)); } @@ -464,7 +461,7 @@ bool QJsonArray::contains(const QJsonValue &value) const */ QJsonValueRef QJsonArray::operator [](qsizetype i) { - Q_ASSERT(a && i >= 0 && i < a->elements.length()); + Q_ASSERT(a && i >= 0 && i < a->elements.size()); return QJsonValueRef(this, i); } @@ -478,36 +475,40 @@ QJsonValue QJsonArray::operator[](qsizetype i) const return at(i); } -/*! - Returns \c true if this array is equal to \a other. - */ -bool QJsonArray::operator==(const QJsonArray &other) const +bool comparesEqual(const QJsonArray &lhs, const QJsonArray &rhs) noexcept { - if (a == other.a) + if (lhs.a == rhs.a) return true; - if (!a) - return !other.a->elements.length(); - if (!other.a) - return !a->elements.length(); - if (a->elements.length() != other.a->elements.length()) + if (!lhs.a) + return !rhs.a->elements.size(); + if (!rhs.a) + return !lhs.a->elements.size(); + if (lhs.a->elements.size() != rhs.a->elements.size()) return false; - for (qsizetype i = 0; i < a->elements.length(); ++i) { - if (a->valueAt(i) != other.a->valueAt(i)) + for (qsizetype i = 0; i < lhs.a->elements.size(); ++i) { + if (lhs.a->valueAt(i) != rhs.a->valueAt(i)) return false; } return true; } -/*! - Returns \c true if this array is not equal to \a other. - */ -bool QJsonArray::operator!=(const QJsonArray &other) const +bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) noexcept { - return !(*this == other); + return lhs == rhs.toArray(); } +/*! \fn bool QJsonArray::operator==(const QJsonArray &lhs, const QJsonArray &rhs) + + Returns \c true if \a lhs array is equal to \a rhs, \c false otherwise. +*/ + +/*! \fn bool QJsonArray::operator!=(const QJsonArray &lhs, const QJsonArray &rhs) + + Returns \c true if \a lhs array is not equal to \a rhs, \c false otherwise. +*/ + /*! \fn QJsonArray::iterator QJsonArray::begin() Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in @@ -602,6 +603,10 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \inmodule QtCore \brief The QJsonArray::iterator class provides an STL-style non-const iterator for QJsonArray. + \compares strong + \compareswith strong QJsonArray::const_iterator + \endcompareswith + QJsonArray::iterator allows you to iterate over a QJsonArray and to modify the array item associated with the iterator. If you want to iterate over a const QJsonArray, use @@ -706,60 +711,60 @@ bool QJsonArray::operator!=(const QJsonArray &other) const */ /*! - \fn bool QJsonArray::iterator::operator==(const iterator &other) const - \fn bool QJsonArray::iterator::operator==(const const_iterator &other) const + \fn bool QJsonArray::iterator::operator==(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::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 QJsonArray::iterator::operator!=(const iterator &other) const - \fn bool QJsonArray::iterator::operator!=(const const_iterator &other) const + \fn bool QJsonArray::iterator::operator!=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::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 QJsonArray::iterator::operator<(const iterator& other) const - \fn bool QJsonArray::iterator::operator<(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator<(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::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 QJsonArray::iterator::operator<=(const iterator& other) const - \fn bool QJsonArray::iterator::operator<=(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator<=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::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 QJsonArray::iterator::operator>(const iterator& other) const - \fn bool QJsonArray::iterator::operator>(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator>(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::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 QJsonArray::iterator::operator>=(const iterator& other) const - \fn bool QJsonArray::iterator::operator>=(const const_iterator& other) const + \fn bool QJsonArray::iterator::operator>=(const iterator &lhs, const iterator &rhs) + \fn bool QJsonArray::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 QJsonArray::iterator &QJsonArray::iterator::operator++() - The prefix ++ operator, \c{++it}, advances the iterator to the + The prefix \c{++} operator, \c{++it}, advances the iterator to the next item in the array and returns an iterator to the new current item. @@ -772,14 +777,14 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \overload - The postfix ++ operator, \c{it++}, advances the iterator to the + The postfix \c{++} operator, \c{it++}, advances the iterator to the next item in the array and returns an iterator to the previously current item. */ /*! \fn QJsonArray::iterator &QJsonArray::iterator::operator--() - The prefix -- operator, \c{--it}, makes the preceding item + The prefix \c{--} operator, \c{--it}, makes the preceding item current and returns an iterator to the new current item. Calling this function on QJsonArray::begin() leads to undefined results. @@ -791,7 +796,7 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \overload - The postfix -- operator, \c{it--}, makes the preceding item + The postfix \c{--} operator, \c{it--}, makes the preceding item current and returns an iterator to the previously current item. */ @@ -837,6 +842,10 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \inmodule QtCore \brief The QJsonArray::const_iterator class provides an STL-style const iterator for QJsonArray. + \compares strong + \compareswith strong QJsonArray::iterator + \endcompareswith + QJsonArray::const_iterator allows you to iterate over a QJsonArray. If you want to modify the QJsonArray as you iterate over it, use QJsonArray::iterator instead. It is generally a @@ -929,53 +938,53 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \sa operator+() */ -/*! \fn bool QJsonArray::const_iterator::operator==(const const_iterator &other) const +/*! \fn bool QJsonArray::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 QJsonArray::const_iterator::operator!=(const const_iterator &other) const +/*! \fn bool QJsonArray::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 QJsonArray::const_iterator::operator<(const const_iterator& other) const + \fn bool QJsonArray::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 QJsonArray::const_iterator::operator<=(const const_iterator& other) const + \fn bool QJsonArray::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 QJsonArray::const_iterator::operator>(const const_iterator& other) const + \fn bool QJsonArray::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 QJsonArray::const_iterator::operator>=(const const_iterator& other) const + \fn bool QJsonArray::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 QJsonArray::const_iterator &QJsonArray::const_iterator::operator++() - The prefix ++ operator, \c{++it}, advances the iterator to the + The prefix \c{++} operator, \c{++it}, advances the iterator to the next item in the array and returns an iterator to the new current item. @@ -988,14 +997,14 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \overload - The postfix ++ operator, \c{it++}, advances the iterator to the + The postfix \c{++} operator, \c{it++}, advances the iterator to the next item in the array and returns an iterator to the previously current item. */ /*! \fn QJsonArray::const_iterator &QJsonArray::const_iterator::operator--() - The prefix -- operator, \c{--it}, makes the preceding item + The prefix \c{--} operator, \c{--it}, makes the preceding item current and returns an iterator to the new current item. Calling this function on QJsonArray::begin() leads to undefined results. @@ -1007,7 +1016,7 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \overload - The postfix -- operator, \c{it--}, makes the preceding item + The postfix \c{--} operator, \c{it--}, makes the preceding item current and returns an iterator to the previously current item. */ @@ -1085,9 +1094,7 @@ QDebug operator<<(QDebug dbg, const QJsonArray &a) #ifndef QT_NO_DATASTREAM QDataStream &operator<<(QDataStream &stream, const QJsonArray &array) { - QJsonDocument doc{array}; - stream << doc.toJson(QJsonDocument::Compact); - return stream; + return stream << QJsonDocument{array}; } QDataStream &operator>>(QDataStream &stream, QJsonArray &array) |