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
13QT_BEGIN_NAMESPACE
14
15class QDebug;
16
17class QCborContainerPrivate;
18
19class Q_CORE_EXPORT QJsonObject
20{
21public:
22 QJsonObject();
23
24 QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args);
25
26 ~QJsonObject();
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&: 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);
58 QJsonValue value(QStringView key) const;
59 QJsonValue value(QLatin1StringView key) const;
60 QJsonValue operator[] (QStringView key) const { return value(key); }
61 QJsonValue operator[] (QLatin1StringView key) const { return value(key); }
62 QJsonValueRef operator[] (QStringView key);
63 QJsonValueRef operator[] (QLatin1StringView key);
64
65 void remove(const QString &key);
66 QJsonValue take(const QString &key);
67 bool contains(const QString &key) const;
68 void remove(QStringView key);
69 void remove(QLatin1StringView key);
70 QJsonValue take(QStringView key);
71 QJsonValue take(QLatin1StringView key);
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
81 class iterator
82 {
83 friend class const_iterator;
84 friend class QJsonObject;
85 QJsonValueRef item;
86
87 public:
88 typedef std::random_access_iterator_tag iterator_category;
89 typedef qsizetype difference_type;
90 typedef QJsonValue value_type;
91 typedef QJsonValueRef reference;
92 typedef QJsonValueRef *pointer;
93
94 inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
95 inline iterator(QJsonObject *obj, qsizetype index) : item(obj, index) { }
96
97 constexpr iterator(const iterator &other) = default;
98 iterator &operator=(const iterator &other)
99 {
100 item.rebind(other: 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: -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)
156 {
157 Q_ASSERT(lhs.item.d == rhs.item.d);
158 return Qt::compareThreeWay(lhs: lhs.item.index, rhs: rhs.item.index);
159 }
160 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
161 const const_iterator &rhs)
162 {
163 Q_ASSERT(lhs.item.d == rhs.item.d);
164 return Qt::compareThreeWay(lhs: lhs.item.index, rhs: 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 }
172 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
173 const iterator &rhs)
174 {
175 return compareThreeWay_helper(lhs, rhs);
176 }
177 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator)
178
179 friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept
180 {
181 return comparesEqual_helper(lhs, rhs);
182 }
183 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
184 const const_iterator &rhs)
185 {
186 return compareThreeWay_helper(lhs, rhs);
187 }
188 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator, const_iterator)
189 };
190 friend class iterator;
191
192 class const_iterator
193 {
194 friend class iterator;
195 QJsonValueConstRef item;
196
197 public:
198 typedef std::random_access_iterator_tag iterator_category;
199 typedef qsizetype difference_type;
200 typedef QJsonValue value_type;
201 typedef const QJsonValueConstRef reference;
202 typedef const QJsonValueConstRef *pointer;
203
204 inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
205 inline const_iterator(const QJsonObject *obj, qsizetype index)
206 : item(const_cast<QJsonObject*>(obj), index) { }
207 inline const_iterator(const iterator &other)
208 : item(other.item) { }
209
210 constexpr const_iterator(const const_iterator &other) = default;
211 const_iterator &operator=(const const_iterator &other)
212 {
213 item.rebind(other: 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: -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)
263 {
264 Q_ASSERT(lhs.item.d == rhs.item.d);
265 return Qt::compareThreeWay(lhs: lhs.item.index, rhs: 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 }
273 friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs,
274 const const_iterator &rhs)
275 {
276 return compareThreeWay_helper(lhs, rhs);
277 }
278 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(const_iterator)
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
292 typedef iterator Iterator;
293 typedef const_iterator ConstIterator;
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
308 typedef QJsonValue mapped_type;
309 typedef QString key_type;
310 typedef qsizetype size_type;
311
312 inline bool empty() const { return isEmpty(); }
313
314private:
315 friend Q_CORE_EXPORT bool comparesEqual(const QJsonObject &lhs,
316 const QJsonObject &rhs);
317 friend bool comparesEqual(const QJsonObject &lhs, const QJsonValue &rhs)
318 {
319 return comparesEqual(lhs, rhs: rhs.toObject());
320 }
321 friend bool comparesEqual(const QJsonObject &lhs, const QJsonValueConstRef &rhs)
322 {
323 return comparesEqual(lhs, rhs: rhs.toObject());
324 }
325 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject)
326 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValue)
327 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValueConstRef)
328 friend class QJsonValue;
329 friend class QJsonDocument;
330 friend class QJsonPrivate::Value;
331 friend class QJsonValueConstRef;
332 friend class QJsonValueRef;
333 friend class QCborMap;
334 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
335
336 QJsonObject(QCborContainerPrivate *object);
337 bool detach(qsizetype reserve = 0);
338
339 template <typename T> QJsonValue valueImpl(T key) const;
340 template <typename T> QJsonValueRef atImpl(T key);
341 template <typename T> void removeImpl(T key);
342 template <typename T> QJsonValue takeImpl(T key);
343 template <typename T> bool containsImpl(T key) const;
344 template <typename T> iterator findImpl(T key);
345 template <typename T> const_iterator constFindImpl(T key) const;
346 template <typename T> iterator insertImpl(T key, const QJsonValue &value);
347
348#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
349 QString keyAt(qsizetype i) const;
350 QJsonValue valueAt(qsizetype i) const;
351 void setValueAt(qsizetype i, const QJsonValue &val);
352#endif
353 void removeAt(qsizetype i);
354 template <typename T> iterator insertAt(qsizetype i, T key, const QJsonValue &val, bool exists);
355
356 QExplicitlySharedDataPointer<QCborContainerPrivate> o;
357};
358
359Q_DECLARE_SHARED(QJsonObject)
360
361#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
362inline QJsonValueConstRef::QJsonValueConstRef(QJsonObject *o, qsizetype idx)
363 : d(o ? o->o.data() : nullptr), is_object(true), index(idx)
364{}
365#endif
366
367Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
368
369#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
370Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
371#endif
372
373#ifndef QT_NO_DATASTREAM
374Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
375Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
376#endif
377
378QT_END_NAMESPACE
379
380#endif // QJSONOBJECT_H
381

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtbase/src/corelib/serialization/qjsonobject.h