Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qjsonobject.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QJSONOBJECT_H
5#define QJSONOBJECT_H
6
7#include <QtCore/qjsonvalue.h>
8#include <QtCore/qiterator.h>
9#include <QtCore/qpair.h>
10#include <QtCore/qshareddata.h>
11#include <initializer_list>
12
14
15class QDebug;
16
18
19class Q_CORE_EXPORT QJsonObject
20{
21public:
23
24 QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args);
25
27
28 QJsonObject(const QJsonObject &other) noexcept;
29 QJsonObject &operator =(const QJsonObject &other) noexcept;
30
31 QJsonObject(QJsonObject &&other) noexcept;
32
33 QJsonObject &operator =(QJsonObject &&other) noexcept
34 {
35 swap(other);
36 return *this;
37 }
38
39 void swap(QJsonObject &other) noexcept
40 {
41 o.swap(other.o);
42 }
43
44 static QJsonObject fromVariantMap(const QVariantMap &map);
45 QVariantMap toVariantMap() const;
46 static QJsonObject fromVariantHash(const QVariantHash &map);
47 QVariantHash toVariantHash() const;
48
49 QStringList keys() const;
50 qsizetype size() const;
51 inline qsizetype count() const { return size(); }
52 inline qsizetype length() const { return size(); }
53 bool isEmpty() const;
54
55 QJsonValue value(const QString &key) const;
56 QJsonValue operator[] (const QString &key) const;
57 QJsonValueRef operator[] (const QString &key);
60 QJsonValue operator[] (QStringView key) const { return value(key); }
61 QJsonValue operator[] (QLatin1StringView key) const { return value(key); }
62 QJsonValueRef operator[] (QStringView key);
64
65 void remove(const QString &key);
66 QJsonValue take(const QString &key);
67 bool contains(const QString &key) const;
72 bool contains(QStringView key) const;
73 bool contains(QLatin1StringView key) const;
74
75#if QT_CORE_REMOVED_SINCE(6, 8)
76 bool operator==(const QJsonObject &other) const;
77 bool operator!=(const QJsonObject &other) const;
78#endif
79 class const_iterator;
80
82 {
83 friend class const_iterator;
84 friend class QJsonObject;
86
87 public:
88 typedef std::random_access_iterator_tag iterator_category;
93
94 inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
96
97 constexpr iterator(const iterator &other) = default;
99 {
100 item.rebind(other.item);
101 return *this;
102 }
103
104 inline QString key() const { return item.objectKey(); }
105 inline QJsonValueRef value() const { return item; }
106 inline QJsonValueRef operator*() const { return item; }
107 inline const QJsonValueConstRef *operator->() const { return &item; }
108 inline QJsonValueRef *operator->() { return &item; }
109 inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); }
110#if QT_CORE_REMOVED_SINCE(6, 8)
111 inline bool operator==(const iterator &other) const
112 { return item.d == other.item.d && item.index == other.item.index; }
113 inline bool operator!=(const iterator &other) const { return !operator==(other); }
114 bool operator<(const iterator& other) const
115 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
116 bool operator<=(const iterator& other) const
117 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
118 bool operator>(const iterator& other) const { return !operator<=(other); }
119 bool operator>=(const iterator& other) const { return !operator<(other); }
120#endif
121 inline iterator &operator++() { ++item.index; return *this; }
122 inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
123 inline iterator &operator--() { --item.index; return *this; }
124 inline iterator operator--(int) { iterator r = *this; --item.index; return r; }
125 inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; }
126 inline iterator operator-(qsizetype j) const { return operator+(-j); }
127 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
128 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
129 qsizetype operator-(iterator j) const { return item.index - j.item.index; }
130
131 public:
132#if QT_CORE_REMOVED_SINCE(6, 8)
133 inline bool operator==(const const_iterator &other) const
134 { return item.d == other.item.d && item.index == other.item.index; }
135 inline bool operator!=(const const_iterator &other) const { return !operator==(other); }
136 bool operator<(const const_iterator& other) const
137 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
138 bool operator<=(const const_iterator& other) const
139 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
140 bool operator>(const const_iterator& other) const { return operator<=(other); }
141 bool operator>=(const const_iterator& other) const { return operator<(other); }
142#endif
143 private:
144 // Helper functions
145 static bool comparesEqual_helper(const iterator &lhs, const iterator &rhs) noexcept
146 {
147 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
148 }
149 static bool comparesEqual_helper(const iterator &lhs, const const_iterator &rhs) noexcept
150 {
151 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
152 }
153
154 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
155 const iterator &rhs) noexcept
156 {
157 Q_ASSERT(lhs.item.d == rhs.item.d);
158 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
159 }
160 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
161 const const_iterator &rhs) noexcept
162 {
163 Q_ASSERT(lhs.item.d == rhs.item.d);
164 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
165 }
166
167 // Compare friends
168 friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
169 {
170 return comparesEqual_helper(lhs, rhs);
171 }
173 const iterator &rhs) noexcept
174 {
175 return compareThreeWay_helper(lhs, rhs);
176 }
178
179 friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept
180 {
181 return comparesEqual_helper(lhs, rhs);
182 }
184 const const_iterator &rhs) noexcept
185 {
186 return compareThreeWay_helper(lhs, rhs);
187 }
189 };
190 friend class iterator;
191
193 {
194 friend class iterator;
196
197 public:
198 typedef std::random_access_iterator_tag iterator_category;
203
204 inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
206 : item(const_cast<QJsonObject*>(obj), index) { }
208 : item(other.item) { }
209
210 constexpr const_iterator(const const_iterator &other) = default;
212 {
213 item.rebind(other.item);
214 return *this;
215 }
216
217 inline QString key() const { return item.objectKey(); }
218 inline QJsonValueConstRef value() const { return item; }
219 inline const QJsonValueConstRef operator*() const { return item; }
220 inline const QJsonValueConstRef *operator->() const { return &item; }
221 inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); }
222#if QT_CORE_REMOVED_SINCE(6, 8)
223 inline bool operator==(const const_iterator &other) const
224 { return item.d == other.item.d && item.index == other.item.index; }
225 inline bool operator!=(const const_iterator &other) const { return !operator==(other); }
226 bool operator<(const const_iterator& other) const
227 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
228 bool operator<=(const const_iterator& other) const
229 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
230 bool operator>(const const_iterator& other) const { return !operator<=(other); }
231 bool operator>=(const const_iterator& other) const { return !operator<(other); }
232#endif
233 inline const_iterator &operator++() { ++item.index; return *this; }
234 inline const_iterator operator++(int) { const_iterator r = *this; ++item.index; return r; }
235 inline const_iterator &operator--() { --item.index; return *this; }
236 inline const_iterator operator--(int) { const_iterator r = *this; --item.index; return r; }
237 inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; }
238 inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
239 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
240 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
241 qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
242#if QT_CORE_REMOVED_SINCE(6, 8)
243 inline bool operator==(const iterator &other) const
244 { return item.d == other.item.d && item.index == other.item.index; }
245 inline bool operator!=(const iterator &other) const { return !operator==(other); }
246 bool operator<(const iterator& other) const
247 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
248 bool operator<=(const iterator& other) const
249 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
250 bool operator>(const iterator& other) const { return !operator<=(other); }
251 bool operator>=(const iterator& other) const { return !operator<(other); }
252#endif
253
254 private:
255 // Helper functions
256 static bool comparesEqual_helper(const const_iterator &lhs,
257 const const_iterator &rhs) noexcept
258 {
259 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
260 }
261 static Qt::strong_ordering compareThreeWay_helper(const const_iterator &lhs,
262 const const_iterator &rhs) noexcept
263 {
264 Q_ASSERT(lhs.item.d == rhs.item.d);
265 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
266 }
267
268 // Compare friends
269 friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
270 {
271 return comparesEqual_helper(lhs, rhs);
272 }
274 const const_iterator &rhs) noexcept
275 {
276 return compareThreeWay_helper(lhs, rhs);
277 }
279 };
280 friend class const_iterator;
281
282 // STL style
283 inline iterator begin() { detach(); return iterator(this, 0); }
284 inline const_iterator begin() const { return const_iterator(this, 0); }
285 inline const_iterator constBegin() const { return const_iterator(this, 0); }
286 inline iterator end() { detach(); return iterator(this, size()); }
287 inline const_iterator end() const { return const_iterator(this, size()); }
288 inline const_iterator constEnd() const { return const_iterator(this, size()); }
289 iterator erase(iterator it);
290
291 // more Qt
294 iterator find(const QString &key);
295 const_iterator find(const QString &key) const { return constFind(key); }
296 const_iterator constFind(const QString &key) const;
297 iterator insert(const QString &key, const QJsonValue &value);
298 iterator find(QStringView key);
299 iterator find(QLatin1StringView key);
300 const_iterator find(QStringView key) const { return constFind(key); }
301 const_iterator find(QLatin1StringView key) const { return constFind(key); }
302 const_iterator constFind(QStringView key) const;
303 const_iterator constFind(QLatin1StringView key) const;
304 iterator insert(QStringView key, const QJsonValue &value);
305 iterator insert(QLatin1StringView key, const QJsonValue &value);
306
307 // STL compatibility
311
312 inline bool empty() const { return isEmpty(); }
313
314private:
315 friend Q_CORE_EXPORT bool comparesEqual(const QJsonObject &lhs,
316 const QJsonObject &rhs) noexcept;
317 friend bool comparesEqual(const QJsonObject &lhs,
318 const QJsonValue &rhs) noexcept
319 {
320 return comparesEqual(lhs, rhs.toObject());
321 }
322 friend bool comparesEqual(const QJsonObject &lhs,
323 const QJsonValueConstRef &rhs) noexcept
324 {
325 return comparesEqual(lhs, rhs.toObject());
326 }
330 friend class QJsonValue;
331 friend class QJsonDocument;
332 friend class QJsonPrivate::Value;
333 friend class QJsonValueConstRef;
334 friend class QJsonValueRef;
335 friend class QCborMap;
336 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
337
339 bool detach(qsizetype reserve = 0);
340
341 template <typename T> QJsonValue valueImpl(T key) const;
342 template <typename T> QJsonValueRef atImpl(T key);
343 template <typename T> void removeImpl(T key);
344 template <typename T> QJsonValue takeImpl(T key);
345 template <typename T> bool containsImpl(T key) const;
346 template <typename T> iterator findImpl(T key);
347 template <typename T> const_iterator constFindImpl(T key) const;
348 template <typename T> iterator insertImpl(T key, const QJsonValue &value);
349
350#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
351 QString keyAt(qsizetype i) const;
353 void setValueAt(qsizetype i, const QJsonValue &val);
354#endif
355 void removeAt(qsizetype i);
356 template <typename T> iterator insertAt(qsizetype i, T key, const QJsonValue &val, bool exists);
357
358 QExplicitlySharedDataPointer<QCborContainerPrivate> o;
359};
360
361Q_DECLARE_SHARED(QJsonObject)
362
363#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
365 : d(o ? o->o.data() : nullptr), is_object(true), index(idx)
366{}
367#endif
368
369Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
370
371#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
372Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
373#endif
374
375#ifndef QT_NO_DATASTREAM
376Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
377Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
378#endif
379
381
382#endif // QJSONOBJECT_H
\inmodule QtCore\reentrant
Definition qcbormap.h:21
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore\reentrant
const QJsonValueConstRef * pointer
const_iterator & operator-=(qsizetype j)
Makes the iterator go back by j items.
const QJsonValueConstRef reference
const_iterator & operator--()
The prefix {–} operator, {–i}, makes the preceding item current and returns an iterator pointing to t...
const QJsonValueConstRef operator*() const
Returns the current item's value.
const_iterator(const QJsonObject *obj, qsizetype index)
constexpr const_iterator(const const_iterator &other)=default
const_iterator operator--(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const_iterator(const iterator &other)
Constructs a copy of other.
std::random_access_iterator_tag iterator_category
A synonym for {std::random_access_iterator_tag} indicating this iterator is a random-access iterator.
QJsonValueConstRef value() const
Returns the current item's value.
const_iterator operator-(qsizetype j) const
Returns an iterator to the item at j positions backward from this iterator.
const_iterator & operator++()
The prefix {++} operator, {++i}, advances the iterator to the next item in the object and returns an ...
QString key() const
Returns the current item's key.
QJsonValueConstRef operator[](qsizetype j) const
Returns the item at offset j from the item pointed to by this iterator (the item at position {*this +...
friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs, const const_iterator &rhs) noexcept
const_iterator operator+(qsizetype j) const
Returns an iterator to the item at j positions forward from this iterator.
const_iterator operator++(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
const_iterator & operator=(const const_iterator &other)
const_iterator & operator+=(qsizetype j)
Advances the iterator by j items.
const_iterator()
Constructs an uninitialized iterator.
const QJsonValueConstRef * operator->() const
Returns a pointer to the current item.
qsizetype operator-(const_iterator j) const
Returns the number of items between the item pointed to by other and the item pointed to by this iter...
\inmodule QtCore\reentrant
Definition qjsonobject.h:82
iterator operator--(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
iterator()
Constructs an uninitialized iterator.
Definition qjsonobject.h:94
QJsonValueRef operator[](qsizetype j) const
Returns a modifiable reference to the item at offset j from the item pointed to by this iterator (the...
QJsonValueRef reference
Definition qjsonobject.h:91
iterator operator-(qsizetype j) const
Returns an iterator to the item at j positions backward from this iterator.
qsizetype operator-(iterator j) const
Returns the number of items between the item pointed to by other and the item pointed to by this iter...
iterator & operator++()
The prefix {++} operator, {++i}, advances the iterator to the next item in the object and returns an ...
QJsonValueRef * pointer
Definition qjsonobject.h:92
QJsonValueRef value() const
Returns a modifiable reference to the current item's value.
iterator operator++(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QJsonValueRef * operator->()
Returns a pointer to a modifiable reference to the current item.
iterator & operator-=(qsizetype j)
Makes the iterator go back by j items.
QString key() const
Returns the current item's key.
friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
qsizetype difference_type
Definition qjsonobject.h:89
iterator & operator--()
The prefix {–} operator, {–i}, makes the preceding item current and returns an iterator pointing to t...
friend Qt::strong_ordering compareThreeWay(const iterator &lhs, const const_iterator &rhs) noexcept
iterator & operator=(const iterator &other)
Definition qjsonobject.h:98
friend Qt::strong_ordering compareThreeWay(const iterator &lhs, const iterator &rhs) noexcept
iterator operator+(qsizetype j) const
Returns an iterator to the item at j positions forward from this iterator.
iterator & operator+=(qsizetype j)
Advances the iterator by j items.
std::random_access_iterator_tag iterator_category
A synonym for {std::random_access_iterator_tag} indicating this iterator is a random-access iterator.
Definition qjsonobject.h:88
const QJsonValueConstRef * operator->() const
Returns a pointer to a constant reference to the current item.
constexpr iterator(const iterator &other)=default
QJsonValueRef operator*() const
Returns a modifiable reference to the current item's value.
iterator(QJsonObject *obj, qsizetype index)
Definition qjsonobject.h:95
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
const_iterator find(QStringView key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
iterator Iterator
Qt-style synonym for QJsonObject::iterator.
~QJsonObject()
Destroys the object.
const_iterator constBegin() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the object.
iterator end()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
QJsonValue mapped_type
Typedef for QJsonValue.
const_iterator end() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool empty() const
This function is provided for STL compatibility.
void swap(QJsonObject &other) noexcept
Definition qjsonobject.h:39
QJsonObject(const QJsonObject &other) noexcept
Creates a copy of other.
QString key_type
Typedef for QString.
qsizetype size_type
Typedef for qsizetype.
const_iterator constEnd() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
const_iterator find(const QString &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const_iterator find(QLatin1StringView key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
qsizetype count() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonobject.h:51
QJsonObject()
Constructs an empty JSON object.
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the object.
friend bool comparesEqual(const QJsonObject &lhs, const QJsonValueConstRef &rhs) noexcept
qsizetype length() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qjsonobject.h:52
const_iterator begin() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
friend bool comparesEqual(const QJsonObject &lhs, const QJsonValue &rhs) noexcept
const_iterator ConstIterator
Qt-style synonym for QJsonObject::const_iterator.
QJsonValueConstRef(const QJsonValueConstRef &)=default
\inmodule QtCore \reentrant
\inmodule QtCore\reentrant
Definition qjsonvalue.h:25
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Definition qcompare.h:400
QMap< QString, QString > map
[6]
cache insert(employee->id(), employee)
QSet< QString >::iterator it
set reserve(20000)
Combined button and popup list for selecting options.
constexpr Qt::strong_ordering compareThreeWay(LeftInt lhs, RightInt rhs) noexcept
qsizetype erase(QByteArray &ba, const T &t)
Definition qbytearray.h:782
#define Q_DECLARE_EQUALITY_COMPARABLE(...)
#define Q_DECLARE_STRONGLY_ORDERED(...)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
constexpr timespec operator+(const timespec &t1, const timespec &t2)
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QJsonObject &)
Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed=0)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &)
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLboolean r
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLhandleARB obj
[2]
GLuint GLfloat * val
bool operator>(const QPoint &a, const QPoint &b)
static qreal valueAt(const QQuickRangeSlider *slider, qreal position)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
unsigned long long quint64
Definition qtypes.h:61
ptrdiff_t qsizetype
Definition qtypes.h:165
bool operator<=(const QUuid &lhs, const QUuid &rhs) noexcept
Definition quuid.h:294
bool operator>=(const QUuid &lhs, const QUuid &rhs) noexcept
Definition quuid.h:296
QStringList keys
settings remove("monkey")
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
this swap(other)
QGraphicsItem * item
QJSValueList args