1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QJSONOBJECT_H
41#define QJSONOBJECT_H
42
43#include <QtCore/qjsonvalue.h>
44#include <QtCore/qiterator.h>
45#include <QtCore/qpair.h>
46#include <QtCore/qshareddata.h>
47#include <initializer_list>
48
49QT_BEGIN_NAMESPACE
50
51class QDebug;
52
53class QCborContainerPrivate;
54
55class Q_CORE_EXPORT QJsonObject
56{
57public:
58 QJsonObject();
59
60 QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args);
61
62 ~QJsonObject();
63
64 QJsonObject(const QJsonObject &other);
65 QJsonObject &operator =(const QJsonObject &other);
66
67 QJsonObject(QJsonObject &&other) noexcept;
68
69 QJsonObject &operator =(QJsonObject &&other) noexcept
70 {
71 swap(other);
72 return *this;
73 }
74
75 void swap(QJsonObject &other) noexcept
76 {
77 qSwap(o, other.o);
78 }
79
80 static QJsonObject fromVariantMap(const QVariantMap &map);
81 QVariantMap toVariantMap() const;
82 static QJsonObject fromVariantHash(const QVariantHash &map);
83 QVariantHash toVariantHash() const;
84
85 QStringList keys() const;
86 qsizetype size() const;
87 inline qsizetype count() const { return size(); }
88 inline qsizetype length() const { return size(); }
89 bool isEmpty() const;
90
91#if QT_STRINGVIEW_LEVEL < 2
92 QJsonValue value(const QString &key) const;
93 QJsonValue operator[] (const QString &key) const;
94 QJsonValueRef operator[] (const QString &key);
95#endif
96 QJsonValue value(QStringView key) const;
97 QJsonValue value(QLatin1String key) const;
98 QJsonValue operator[] (QStringView key) const { return value(key); }
99 QJsonValue operator[] (QLatin1String key) const { return value(key); }
100 QJsonValueRef operator[] (QStringView key);
101 QJsonValueRef operator[] (QLatin1String key);
102
103#if QT_STRINGVIEW_LEVEL < 2
104 void remove(const QString &key);
105 QJsonValue take(const QString &key);
106 bool contains(const QString &key) const;
107#endif
108 void remove(QStringView key);
109 void remove(QLatin1String key);
110 QJsonValue take(QStringView key);
111 QJsonValue take(QLatin1String key);
112 bool contains(QStringView key) const;
113 bool contains(QLatin1String key) const;
114
115 bool operator==(const QJsonObject &other) const;
116 bool operator!=(const QJsonObject &other) const;
117
118 class const_iterator;
119
120 class iterator
121 {
122 friend class const_iterator;
123 friend class QJsonObject;
124 mutable QJsonValueRef item;
125
126 public:
127 typedef std::random_access_iterator_tag iterator_category;
128 typedef qsizetype difference_type;
129 typedef QJsonValue value_type;
130 typedef QJsonValueRef reference;
131 typedef QJsonValueRef *pointer;
132
133 inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
134 inline iterator(QJsonObject *obj, qsizetype index) : item(obj, index) { }
135
136 constexpr iterator(const iterator &other) = default;
137 iterator &operator=(const iterator &other)
138 {
139 item.o = other.item.o;
140 item.index = other.item.index;
141 return *this;
142 }
143
144 inline QString key() const { return item.o->keyAt(item.index); }
145 inline QJsonValueRef value() const { return item; }
146 inline QJsonValueRef operator*() const { return item; }
147 inline QJsonValueRef *operator->() const { return &item; }
148 const QJsonValueRef operator[](qsizetype j) { return { item.o, qsizetype(item.index) + j }; }
149
150 inline bool operator==(const iterator &other) const
151 { return item.o == other.item.o && item.index == other.item.index; }
152 inline bool operator!=(const iterator &other) const { return !(*this == other); }
153 bool operator<(const iterator& other) const
154 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
155 bool operator<=(const iterator& other) const
156 { Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
157 bool operator>(const iterator& other) const { return !(*this <= other); }
158 bool operator>=(const iterator& other) const { return !(*this < other); }
159
160 inline iterator &operator++() { ++item.index; return *this; }
161 inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
162 inline iterator &operator--() { --item.index; return *this; }
163 inline iterator operator--(int) { iterator r = *this; --item.index; return r; }
164 inline iterator operator+(qsizetype j) const
165 { iterator r = *this; r.item.index += quint64(j); return r; }
166 inline iterator operator-(qsizetype j) const { return operator+(-j); }
167 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
168 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
169 qsizetype operator-(iterator j) const { return item.index - j.item.index; }
170
171 public:
172 inline bool operator==(const const_iterator &other) const
173 { return item.o == other.item.o && item.index == other.item.index; }
174 inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
175 bool operator<(const const_iterator& other) const
176 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
177 bool operator<=(const const_iterator& other) const
178 { Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
179 bool operator>(const const_iterator& other) const { return !(*this <= other); }
180 bool operator>=(const const_iterator& other) const { return !(*this < other); }
181 };
182 friend class iterator;
183
184 class const_iterator
185 {
186 friend class iterator;
187 QJsonValueRef item;
188
189 public:
190 typedef std::random_access_iterator_tag iterator_category;
191 typedef qsizetype difference_type;
192 typedef QJsonValue value_type;
193 typedef const QJsonValueRef reference;
194 typedef const QJsonValueRef *pointer;
195
196 inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
197 inline const_iterator(const QJsonObject *obj, qsizetype index)
198 : item(const_cast<QJsonObject*>(obj), index) { }
199 inline const_iterator(const iterator &other)
200 : item(other.item) { }
201
202 constexpr const_iterator(const const_iterator &other) = default;
203 const_iterator &operator=(const const_iterator &other)
204 {
205 item.o = other.item.o;
206 item.index = other.item.index;
207 return *this;
208 }
209
210 inline QString key() const { return item.o->keyAt(item.index); }
211 inline QJsonValueRef value() const { return item; }
212 inline const QJsonValueRef operator*() const { return item; }
213 inline const QJsonValueRef *operator->() const { return &item; }
214 const QJsonValueRef operator[](qsizetype j) { return { item.o, qsizetype(item.index) + j }; }
215
216 inline bool operator==(const const_iterator &other) const
217 { return item.o == other.item.o && item.index == other.item.index; }
218 inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
219 bool operator<(const const_iterator& other) const
220 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
221 bool operator<=(const const_iterator& other) const
222 { Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
223 bool operator>(const const_iterator& other) const { return !(*this <= other); }
224 bool operator>=(const const_iterator& other) const { return !(*this < other); }
225
226 inline const_iterator &operator++() { ++item.index; return *this; }
227 inline const_iterator operator++(int) { const_iterator r = *this; ++item.index; return r; }
228 inline const_iterator &operator--() { --item.index; return *this; }
229 inline const_iterator operator--(int) { const_iterator r = *this; --item.index; return r; }
230 inline const_iterator operator+(qsizetype j) const
231 { const_iterator r = *this; r.item.index += quint64(j); return r; }
232 inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
233 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
234 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
235 qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
236
237 inline bool operator==(const iterator &other) const
238 { return item.o == other.item.o && item.index == other.item.index; }
239 inline bool operator!=(const iterator &other) const { return !(*this == other); }
240 bool operator<(const iterator& other) const
241 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
242 bool operator<=(const iterator& other) const
243 { Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
244 bool operator>(const iterator& other) const { return !(*this <= other); }
245 bool operator>=(const iterator& other) const { return !(*this < other); }
246 };
247 friend class const_iterator;
248
249 // STL style
250 inline iterator begin() { detach(); return iterator(this, 0); }
251 inline const_iterator begin() const { return const_iterator(this, 0); }
252 inline const_iterator constBegin() const { return const_iterator(this, 0); }
253 inline iterator end() { detach(); return iterator(this, size()); }
254 inline const_iterator end() const { return const_iterator(this, size()); }
255 inline const_iterator constEnd() const { return const_iterator(this, size()); }
256 iterator erase(iterator it);
257
258 // more Qt
259 typedef iterator Iterator;
260 typedef const_iterator ConstIterator;
261#if QT_STRINGVIEW_LEVEL < 2
262 iterator find(const QString &key);
263 const_iterator find(const QString &key) const { return constFind(key); }
264 const_iterator constFind(const QString &key) const;
265 iterator insert(const QString &key, const QJsonValue &value);
266#endif
267 iterator find(QStringView key);
268 iterator find(QLatin1String key);
269 const_iterator find(QStringView key) const { return constFind(key); }
270 const_iterator find(QLatin1String key) const { return constFind(key); }
271 const_iterator constFind(QStringView key) const;
272 const_iterator constFind(QLatin1String key) const;
273 iterator insert(QStringView key, const QJsonValue &value);
274 iterator insert(QLatin1String key, const QJsonValue &value);
275
276 // STL compatibility
277 typedef QJsonValue mapped_type;
278 typedef QString key_type;
279 typedef qsizetype size_type;
280
281 inline bool empty() const { return isEmpty(); }
282
283private:
284 friend class QJsonValue;
285 friend class QJsonDocument;
286 friend class QJsonValueRef;
287 friend class QCborMap;
288 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
289
290 QJsonObject(QCborContainerPrivate *object);
291 bool detach(qsizetype reserve = 0);
292
293 template <typename T> QJsonValue valueImpl(T key) const;
294 template <typename T> QJsonValueRef atImpl(T key);
295 template <typename T> void removeImpl(T key);
296 template <typename T> QJsonValue takeImpl(T key);
297 template <typename T> bool containsImpl(T key) const;
298 template <typename T> iterator findImpl(T key);
299 template <typename T> const_iterator constFindImpl(T key) const;
300 template <typename T> iterator insertImpl(T key, const QJsonValue &value);
301
302 QString keyAt(qsizetype i) const;
303 QJsonValue valueAt(qsizetype i) const;
304 void setValueAt(qsizetype i, const QJsonValue &val);
305 void removeAt(qsizetype i);
306 template <typename T> iterator insertAt(qsizetype i, T key, const QJsonValue &val, bool exists);
307
308 QExplicitlySharedDataPointer<QCborContainerPrivate> o;
309};
310
311Q_DECLARE_SHARED(QJsonObject)
312
313Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
314
315#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
316Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
317#endif
318
319#ifndef QT_NO_DATASTREAM
320Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
321Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
322#endif
323
324QT_END_NAMESPACE
325
326#endif // QJSONOBJECT_H
327

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