diff options
| author | Johannes Grunenberg <nerixdev@outlook.de> | 2025-05-16 19:46:37 +0200 |
|---|---|---|
| committer | Thiago Macieira <thiago.macieira@intel.com> | 2025-06-18 18:03:58 +0000 |
| commit | e97b9cd671003e2b2ab2f66bc649cc76fcc3a9a0 (patch) | |
| tree | 5cee31ee121c2e26f4a75481c74ca89b01467c77 /src/corelib/serialization/qjsonobject.cpp | |
| parent | 522d505dd81834d22d366d0f36a26451dedafe11 (diff) | |
QJsonObject/QCborMap: Add asKeyValueRange()
This adds {QJsonObject,QCborMap}::asKeyValueRange() which returns a
range over key-value pairs of the object/map
(`pair<QAnyStringView, QJsonValueRef>` and
`pair<QCborValueConstRef, QCborValue>`).
This uses QKeyValueIterator under the hood. QJsonObject's iterator only
iterates over the items, so using it in a range-based for loop won't
give users access to the key. With `asKeyValueRange` one can iterate
over both keys and values and conveniently use structured bindings.
QCborMap's iterator already iterates over key-value pairs, so
`asKeyValueRange` is provided for API symmetry.
In `QKeyValueIterator`, this adds a fourth template parameter `Traits`
to support custom `key()` and `value()` functions.
This is specifically needed for `QJsonObject`, as its actualy key is a
string view, but `key()` returns a `QString`.
[ChangeLog][QtCore][QJsonObject] Added asKeyValueRange to iterate with a
range-based for loop over key-value pairs with support for structured
bindings.
[ChangeLog][QtCore][QCborMap] Added asKeyValueRange to iterate with a
range-based for loop over key-value pairs with support for structured
bindings.
Pick-to: 6.10
Change-Id: I68d97fada8b2d7ef7224f1beb5aa685aac3d1b16
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/serialization/qjsonobject.cpp')
| -rw-r--r-- | src/corelib/serialization/qjsonobject.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index 3e0857dc954..60b2cb3bd0f 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -831,6 +831,107 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const returning \c false. */ +/*! \typedef QJsonObject::const_key_value_iterator + \inmodule QtCore + \since 6.10 + \brief The QJsonObject::const_key_value_iterator typedef provides an STL-style iterator for + QJsonObject. + + QJsonObject::const_key_value_iterator is essentially the same as QJsonObject::const_iterator + with the difference that operator*() returns a key/value pair instead of a + value. + + \sa QKeyValueIterator +*/ + +/*! \typedef QJsonObject::key_value_iterator + \inmodule QtCore + \since 6.10 + \brief The QJsonObject::key_value_iterator typedef provides an STL-style iterator for + QJsonObject. + + QJsonObject::key_value_iterator is essentially the same as QJsonObject::iterator + with the difference that operator*() returns a key/value pair instead of a + value. + + \sa QKeyValueIterator +*/ + +/*! \fn QJsonObject::key_value_iterator QJsonObject::keyValueBegin() + \since 6.10 + + Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first entry + in the object. + + \sa keyValueEnd() +*/ + +/*! \fn QJsonObject::key_value_iterator QJsonObject::keyValueEnd() + \since 6.10 + + Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + entry after the last entry in the object. + + \sa keyValueBegin() +*/ + +/*! \fn QJsonObject::const_key_value_iterator QJsonObject::keyValueBegin() const + \since 6.10 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first entry + in the object. + + \sa keyValueEnd() +*/ + +/*! \fn QJsonObject::const_key_value_iterator QJsonObject::constKeyValueBegin() const + \since 6.10 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first entry + in the object. + + \sa keyValueBegin() +*/ + +/*! \fn QJsonObject::const_key_value_iterator QJsonObject::keyValueEnd() const + \since 6.10 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + entry after the last entry in the object. + + \sa keyValueBegin() +*/ + +/*! \fn QJsonObject::const_key_value_iterator QJsonObject::constKeyValueEnd() const + \since 6.10 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + entry after the last entry in the ibject. + + \sa constKeyValueBegin() +*/ + +/*! \fn auto QJsonObject::asKeyValueRange() & + \fn auto QJsonObject::asKeyValueRange() const & + \fn auto QJsonObject::asKeyValueRange() && + \fn auto QJsonObject::asKeyValueRange() const && + \since 6.10 + + Returns a range object that allows iteration over this object as + key/value pairs. For instance, this range object can be used in a + range-based for loop, in combination with a structured binding declaration: + + \snippet code/src_corelib_serialization_qjsonobject.cpp 1 + + Note that the value obtained this way is a reference into the one in the + object. Specifically, mutating the value will modify the object itself. + + When calling this method on rvalues (e.g. on a temporary created in the + inializer of a ranged for-loop), the object will be captured in this range. + + \sa QKeyValueIterator +*/ + /*! \class QJsonObject::iterator \inmodule QtCore \ingroup json |
