Qt 6.x
The Qt SDK
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<QPair<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 bool operator==(const QJsonObject &other) const;
76 bool operator!=(const QJsonObject &other) const;
77
78 class const_iterator;
79
81 {
82 friend class const_iterator;
83 friend class QJsonObject;
85
86 public:
87 typedef std::random_access_iterator_tag iterator_category;
92
93 inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
95
96 constexpr iterator(const iterator &other) = default;
98 {
99 item.rebind(other.item);
100 return *this;
101 }
102
103 inline QString key() const { return item.objectKey(); }
104 inline QJsonValueRef value() const { return item; }
105 inline QJsonValueRef operator*() const { return item; }
106 inline const QJsonValueConstRef *operator->() const { return &item; }
107 inline QJsonValueRef *operator->() { return &item; }
108 inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); }
109
110 inline bool operator==(const iterator &other) const
111 { return item.d == other.item.d && item.index == other.item.index; }
112 inline bool operator!=(const iterator &other) const { return !(*this == other); }
113 bool operator<(const iterator& other) const
114 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
115 bool operator<=(const iterator& other) const
116 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
117 bool operator>(const iterator& other) const { return !(*this <= other); }
118 bool operator>=(const iterator& other) const { return !(*this < other); }
119
120 inline iterator &operator++() { ++item.index; return *this; }
121 inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
122 inline iterator &operator--() { --item.index; return *this; }
123 inline iterator operator--(int) { iterator r = *this; --item.index; return r; }
124 inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; }
125 inline iterator operator-(qsizetype j) const { return operator+(-j); }
126 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
127 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
128 qsizetype operator-(iterator j) const { return item.index - j.item.index; }
129
130 public:
131 inline bool operator==(const const_iterator &other) const
132 { return item.d == other.item.d && item.index == other.item.index; }
133 inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
134 bool operator<(const const_iterator& other) const
135 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
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 { return !(*this <= other); }
139 bool operator>=(const const_iterator& other) const { return !(*this < other); }
140 };
141 friend class iterator;
142
144 {
145 friend class iterator;
147
148 public:
149 typedef std::random_access_iterator_tag iterator_category;
154
155 inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
157 : item(const_cast<QJsonObject*>(obj), index) { }
159 : item(other.item) { }
160
161 constexpr const_iterator(const const_iterator &other) = default;
163 {
164 item.rebind(other.item);
165 return *this;
166 }
167
168 inline QString key() const { return item.objectKey(); }
169 inline QJsonValueConstRef value() const { return item; }
170 inline const QJsonValueConstRef operator*() const { return item; }
171 inline const QJsonValueConstRef *operator->() const { return &item; }
172 inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); }
173
174 inline bool operator==(const const_iterator &other) const
175 { return item.d == other.item.d && item.index == other.item.index; }
176 inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
177 bool operator<(const const_iterator& other) const
178 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
179 bool operator<=(const const_iterator& other) const
180 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
181 bool operator>(const const_iterator& other) const { return !(*this <= other); }
182 bool operator>=(const const_iterator& other) const { return !(*this < other); }
183
184 inline const_iterator &operator++() { ++item.index; return *this; }
185 inline const_iterator operator++(int) { const_iterator r = *this; ++item.index; return r; }
186 inline const_iterator &operator--() { --item.index; return *this; }
187 inline const_iterator operator--(int) { const_iterator r = *this; --item.index; return r; }
188 inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; }
189 inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
190 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
191 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
192 qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
193
194 inline bool operator==(const iterator &other) const
195 { return item.d == other.item.d && item.index == other.item.index; }
196 inline bool operator!=(const iterator &other) const { return !(*this == other); }
197 bool operator<(const iterator& other) const
198 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
199 bool operator<=(const iterator& other) const
200 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
201 bool operator>(const iterator& other) const { return !(*this <= other); }
202 bool operator>=(const iterator& other) const { return !(*this < other); }
203 };
204 friend class const_iterator;
205
206 // STL style
207 inline iterator begin() { detach(); return iterator(this, 0); }
208 inline const_iterator begin() const { return const_iterator(this, 0); }
209 inline const_iterator constBegin() const { return const_iterator(this, 0); }
210 inline iterator end() { detach(); return iterator(this, size()); }
211 inline const_iterator end() const { return const_iterator(this, size()); }
212 inline const_iterator constEnd() const { return const_iterator(this, size()); }
213 iterator erase(iterator it);
214
215 // more Qt
218 iterator find(const QString &key);
219 const_iterator find(const QString &key) const { return constFind(key); }
220 const_iterator constFind(const QString &key) const;
221 iterator insert(const QString &key, const QJsonValue &value);
222 iterator find(QStringView key);
223 iterator find(QLatin1StringView key);
224 const_iterator find(QStringView key) const { return constFind(key); }
225 const_iterator find(QLatin1StringView key) const { return constFind(key); }
226 const_iterator constFind(QStringView key) const;
227 const_iterator constFind(QLatin1StringView key) const;
228 iterator insert(QStringView key, const QJsonValue &value);
229 iterator insert(QLatin1StringView key, const QJsonValue &value);
230
231 // STL compatibility
235
236 inline bool empty() const { return isEmpty(); }
237
238private:
239 friend class QJsonValue;
240 friend class QJsonDocument;
242 friend class QJsonValueConstRef;
243 friend class QJsonValueRef;
244 friend class QCborMap;
245 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
246
248 bool detach(qsizetype reserve = 0);
249
250 template <typename T> QJsonValue valueImpl(T key) const;
251 template <typename T> QJsonValueRef atImpl(T key);
252 template <typename T> void removeImpl(T key);
253 template <typename T> QJsonValue takeImpl(T key);
254 template <typename T> bool containsImpl(T key) const;
255 template <typename T> iterator findImpl(T key);
256 template <typename T> const_iterator constFindImpl(T key) const;
257 template <typename T> iterator insertImpl(T key, const QJsonValue &value);
258
259#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
260 QString keyAt(qsizetype i) const;
262 void setValueAt(qsizetype i, const QJsonValue &val);
263#endif
264 void removeAt(qsizetype i);
265 template <typename T> iterator insertAt(qsizetype i, T key, const QJsonValue &val, bool exists);
266
268};
269
270Q_DECLARE_SHARED(QJsonObject)
271
272#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
274 : d(o ? o->o.data() : nullptr), is_object(true), index(idx)
275{}
276#endif
277
278Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
279
280#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
281Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
282#endif
283
284#ifndef QT_NO_DATASTREAM
285Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
286Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
287#endif
288
290
291#endif // QJSONOBJECT_H
\inmodule QtCore\reentrant
Definition qcbormap.h:21
\inmodule QtCore\reentrant
Definition qdatastream.h:30
\inmodule QtCore
\inmodule QtCore\reentrant
bool operator==(const const_iterator &other) const
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 ...
bool operator==(const iterator &other) const
Returns true if other points to the same item as this iterator; otherwise returns false.
QString key() const
Returns the current item's key.
bool operator<(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than the item pointed to by the other it...
QJsonValueConstRef operator[](qsizetype j) const
Returns the item at offset j from the item pointed to by this iterator (the item at position {*this +...
bool operator>(const iterator &other) const
bool operator<=(const iterator &other) const
bool operator<=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than or equal to the item pointed to by ...
bool operator>=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than or equal to the item pointed to ...
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...
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...
bool operator!=(const iterator &other) const
Returns true if other points to a different item than this iterator; otherwise returns false.
bool operator<(const iterator &other) const
bool operator>=(const iterator &other) const
bool operator>(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than the item pointed to by the other...
bool operator!=(const const_iterator &other) const
\inmodule QtCore\reentrant
Definition qjsonobject.h:81
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:93
bool operator<=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than or equal to the item pointed to by ...
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:90
bool operator!=(const const_iterator &other) const
Returns true if other points to a different item than this iterator; otherwise returns false.
bool operator<=(const iterator &other) const
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 ...
bool operator==(const const_iterator &other) const
Returns true if other points to the same item as this iterator; otherwise returns false.
QJsonValueRef * pointer
Definition qjsonobject.h:91
bool operator>=(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than or equal to the item pointed to ...
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...
bool operator>(const iterator &other) const
bool operator<(const const_iterator &other) const
Returns true if the item pointed to by this iterator is less than the item pointed to by the other it...
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.
bool operator>(const const_iterator &other) const
Returns true if the item pointed to by this iterator is greater than the item pointed to by the other...
QString key() const
Returns the current item's key.
qsizetype difference_type
Definition qjsonobject.h:88
bool operator==(const iterator &other) const
iterator & operator--()
The prefix {–} operator, {–i}, makes the preceding item current and returns an iterator pointing to t...
iterator & operator=(const iterator &other)
Definition qjsonobject.h:97
bool operator>=(const iterator &other) const
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:87
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:94
bool operator!=(const iterator &other) const
bool operator<(const iterator &other) const
\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.
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...
const_iterator ConstIterator
Qt-style synonym for QJsonObject::const_iterator.
QJsonValueConstRef(const QJsonValueConstRef &)=default
\inmodule QtCore \reentrant
\inmodule QtCore\reentrant
Definition qjsonvalue.h:24
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
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.
qsizetype erase(QByteArray &ba, const T &t)
Definition qbytearray.h:695
std::pair< T1, T2 > QPair
constexpr bool operator!=(const timespec &t1, const timespec &t2)
constexpr timespec operator+(const timespec &t1, const timespec &t2)
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
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:1219
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned long long quint64
Definition qtypes.h:56
ptrdiff_t qsizetype
Definition qtypes.h:70
QStringList keys
settings remove("monkey")
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
this swap(other)
QGraphicsItem * item
QJSValueList args