diff options
Diffstat (limited to 'src/corelib/serialization/qjsonarray.cpp')
-rw-r--r-- | src/corelib/serialization/qjsonarray.cpp | 227 |
1 files changed, 99 insertions, 128 deletions
diff --git a/src/corelib/serialization/qjsonarray.cpp b/src/corelib/serialization/qjsonarray.cpp index bf678a0bd8..0c1b0ac7c8 100644 --- a/src/corelib/serialization/qjsonarray.cpp +++ b/src/corelib/serialization/qjsonarray.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> @@ -58,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. @@ -75,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} */ /*! @@ -155,7 +124,6 @@ QJsonArray::QJsonArray() = default; QJsonArray::QJsonArray(QCborContainerPrivate *array) : a(array) { - Q_ASSERT(array); } /*! @@ -175,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) { @@ -189,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) @@ -256,6 +215,7 @@ QJsonArray QJsonArray::fromStringList(const QStringList &list) return array; } +#ifndef QT_NO_VARIANT /*! Converts the variant list \a list to a QJsonArray. @@ -280,6 +240,7 @@ QVariantList QJsonArray::toVariantList() const { return QCborArray::fromJsonArray(*this).toVariantList(); } +#endif // !QT_NO_VARIANT /*! @@ -376,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); @@ -412,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(); @@ -431,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)); } @@ -466,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)); } @@ -500,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); } @@ -514,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 @@ -638,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 @@ -742,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. @@ -808,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. @@ -827,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. */ @@ -873,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 @@ -965,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. @@ -1024,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. @@ -1043,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. */ @@ -1121,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) |