summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qjsonobject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/serialization/qjsonobject.cpp')
-rw-r--r--src/corelib/serialization/qjsonobject.cpp219
1 files changed, 101 insertions, 118 deletions
diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp
index ad217b419e..2f61de0824 100644
--- a/src/corelib/serialization/qjsonobject.cpp
+++ b/src/corelib/serialization/qjsonobject.cpp
@@ -25,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.
@@ -42,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}
*/
/*!
@@ -84,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:
@@ -103,7 +108,6 @@ QJsonObject::QJsonObject() = default;
QJsonObject::QJsonObject(QCborContainerPrivate *object)
: o(object)
{
- Q_ASSERT(o);
}
/*!
@@ -111,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);
@@ -123,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)
{
@@ -137,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;
-}
+QJsonObject &QJsonObject::operator =(const QJsonObject &other) noexcept = default;
-/*!
- \fn QJsonObject::QJsonObject(QJsonObject &&other)
- \since 5.10
-
- Move-constructs a QJsonObject from \a other.
-*/
/*!
\fn QJsonObject &QJsonObject::operator =(QJsonObject &&other)
@@ -164,7 +160,7 @@ 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.
@@ -227,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 (qsizetype 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;
@@ -249,7 +246,7 @@ QStringList QJsonObject::keys() const
*/
qsizetype QJsonObject::size() const
{
- return o ? o->elements.length() / 2 : 0;
+ return o ? o->elements.size() / 2 : 0;
}
/*!
@@ -272,14 +269,13 @@ static qsizetype indexOf(const QExplicitlySharedDataPointer<QCborContainerPrivat
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.it - begin.it;
}
-#if QT_STRINGVIEW_LEVEL < 2
/*!
Returns a QJsonValue representing the value for the key \a key.
@@ -291,7 +287,6 @@ QJsonValue QJsonObject::value(const QString &key) const
{
return value(QStringView(key));
}
-#endif
/*!
\overload
@@ -327,7 +322,6 @@ QJsonValue QJsonObject::valueImpl(T key) const
return QJsonPrivate::Value::fromTrustedCbor(o->valueAt(i + 1));
}
-#if QT_STRINGVIEW_LEVEL < 2
/*!
Returns a QJsonValue representing the value for the key \a key.
@@ -341,7 +335,6 @@ QJsonValue QJsonObject::operator [](const QString &key) const
{
return (*this)[QStringView(key)];
}
-#endif
/*!
\fn QJsonValue QJsonObject::operator [](QStringView key) const
@@ -357,7 +350,6 @@ QJsonValue QJsonObject::operator [](const QString &key) const
\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
@@ -375,7 +367,6 @@ QJsonValueRef QJsonObject::operator [](const QString &key)
{
return (*this)[QStringView(key)];
}
-#endif
/*!
\overload
@@ -407,7 +398,7 @@ QJsonValueRef QJsonObject::atImpl(T key)
bool keyExists = false;
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()));
}
@@ -415,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.
@@ -433,7 +423,6 @@ QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &
{
return insert(QStringView(key), value);
}
-#endif
/*!
\overload
@@ -475,7 +464,7 @@ template <typename T>
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;
@@ -488,7 +477,6 @@ QJsonObject::iterator QJsonObject::insertAt(qsizetype pos, T key, const QJsonVal
return {this, pos / 2};
}
-#if QT_STRINGVIEW_LEVEL < 2
/*!
Removes \a key from the object.
@@ -498,7 +486,6 @@ void QJsonObject::remove(const QString &key)
{
remove(QStringView(key));
}
-#endif
/*!
\overload
@@ -535,7 +522,6 @@ void QJsonObject::removeImpl(T key)
removeAt(index);
}
-#if QT_STRINGVIEW_LEVEL < 2
/*!
Removes \a key from the object.
@@ -549,7 +535,6 @@ QJsonValue QJsonObject::take(const QString &key)
{
return take(QStringView(key));
}
-#endif
/*!
\overload
@@ -589,7 +574,6 @@ QJsonValue QJsonObject::takeImpl(T key)
return v;
}
-#if QT_STRINGVIEW_LEVEL < 2
/*!
Returns \c true if the object contains key \a key.
@@ -599,7 +583,6 @@ bool QJsonObject::contains(const QString &key) const
{
return contains(QStringView(key));
}
-#endif
/*!
\overload
@@ -634,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 (qsizetype 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;
}
@@ -657,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
@@ -681,7 +664,6 @@ QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it)
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.
@@ -693,7 +675,6 @@ QJsonObject::iterator QJsonObject::find(const QString &key)
{
return find(QStringView(key));
}
-#endif
/*!
\overload
@@ -727,12 +708,10 @@ QJsonObject::iterator QJsonObject::findImpl(T key)
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
@@ -746,7 +725,6 @@ QJsonObject::iterator QJsonObject::findImpl(T key)
\since 5.7
*/
-#if QT_STRINGVIEW_LEVEL < 2
/*!
Returns a const iterator pointing to the item with key \a key in the
map.
@@ -758,7 +736,6 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const
{
return constFind(QStringView(key));
}
-#endif
/*!
\overload
@@ -863,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
@@ -879,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
@@ -996,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.
@@ -1062,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
@@ -1082,7 +1063,7 @@ 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.
*/
@@ -1133,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
@@ -1149,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
@@ -1241,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.
@@ -1302,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
@@ -1322,7 +1305,7 @@ 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.
*/
@@ -1381,7 +1364,7 @@ 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;
}
@@ -1391,7 +1374,7 @@ bool QJsonObject::detach(qsizetype reserve)
*/
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);
}
@@ -1400,7 +1383,7 @@ QString QJsonObject::keyAt(qsizetype 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));
}
@@ -1410,7 +1393,7 @@ QJsonValue QJsonObject::valueAt(qsizetype i) const
*/
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);