summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets
diff options
context:
space:
mode:
authorJohannes Grunenberg <nerixdev@outlook.de>2025-05-16 19:46:37 +0200
committerThiago Macieira <thiago.macieira@intel.com>2025-06-18 18:03:58 +0000
commite97b9cd671003e2b2ab2f66bc649cc76fcc3a9a0 (patch)
tree5cee31ee121c2e26f4a75481c74ca89b01467c77 /src/corelib/doc/snippets
parent522d505dd81834d22d366d0f36a26451dedafe11 (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/doc/snippets')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_serialization_qjsonobject.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_serialization_qjsonobject.cpp b/src/corelib/doc/snippets/code/src_corelib_serialization_qjsonobject.cpp
new file mode 100644
index 00000000000..a6a1cba7322
--- /dev/null
+++ b/src/corelib/doc/snippets/code/src_corelib_serialization_qjsonobject.cpp
@@ -0,0 +1,17 @@
+// Copyright (C) 2025 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+//! [1]
+QJsonObject obj{
+ { "something", "is" },
+ { "in", "this" },
+ { "object", 42 },
+};
+
+for (auto [key, value] : obj.asKeyValueRange()) {
+ qDebug() << key << "->" << value;
+ if (key == "object")
+ value = "!"; // modify the object at this key
+}
+qDebug() << obj["object"]; // QJsonValue(string, "!")
+//! [1]