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;
52template <class Key, class T> class QMap;
53typedef QMap<QString, QVariant> QVariantMap;
54template <class Key, class T> class QHash;
55typedef QHash<QString, QVariant> QVariantHash;
56
57class QCborContainerPrivate;
58
59class Q_CORE_EXPORT QJsonObject
60{
61public:
62 QJsonObject();
63
64 QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args);
65
66 ~QJsonObject();
67
68 QJsonObject(const QJsonObject &other);
69 QJsonObject &operator =(const QJsonObject &other);
70
71 QJsonObject(QJsonObject &&other) noexcept;
72
73 QJsonObject &operator =(QJsonObject &&other) noexcept
74 {
75 swap(other);
76 return *this;
77 }
78
79 void swap(QJsonObject &other) noexcept
80 {
81 qSwap(o, other.o);
82 }
83
84 static QJsonObject fromVariantMap(const QVariantMap &map);
85 QVariantMap toVariantMap() const;
86 static QJsonObject fromVariantHash(const QVariantHash &map);
87 QVariantHash toVariantHash() const;
88
89 QStringList keys() const;
90 qsizetype size() const;
91 inline qsizetype count() const { return size(); }
92 inline qsizetype length() const { return size(); }
93 bool isEmpty() const;
94
95#if QT_STRINGVIEW_LEVEL < 2
96 QJsonValue value(const QString &key) const;
97 QJsonValue operator[] (const QString &key) const;
98 QJsonValueRef operator[] (const QString &key);
99#endif
100 QJsonValue value(QStringView key) const;
101 QJsonValue value(QLatin1String key) const;
102 QJsonValue operator[] (QStringView key) const { return value(key); }
103 QJsonValue operator[] (QLatin1String key) const { return value(key); }
104 QJsonValueRef operator[] (QStringView key);
105 QJsonValueRef operator[] (QLatin1String key);
106
107#if QT_STRINGVIEW_LEVEL < 2
108 void remove(const QString &key);
109 QJsonValue take(const QString &key);
110 bool contains(const QString &key) const;
111#endif
112 void remove(QStringView key);
113 void remove(QLatin1String key);
114 QJsonValue take(QStringView key);
115 QJsonValue take(QLatin1String key);
116 bool contains(QStringView key) const;
117 bool contains(QLatin1String key) const;
118
119 bool operator==(const QJsonObject &other) const;
120 bool operator!=(const QJsonObject &other) const;
121
122 class const_iterator;
123
124 class iterator
125 {
126 friend class const_iterator;
127 friend class QJsonObject;
128 mutable QJsonValueRef item;
129
130 public:
131 typedef std::random_access_iterator_tag iterator_category;
132 typedef qsizetype difference_type;
133 typedef QJsonValue value_type;
134 typedef QJsonValueRef reference;
135 typedef QJsonValueRef *pointer;
136
137 inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
138 inline iterator(QJsonObject *obj, qsizetype index) : item(obj, index) { }
139
140 constexpr iterator(const iterator &other) = default;
141 iterator &operator=(const iterator &other)
142 {
143 item.o = other.item.o;
144 item.index = other.item.index;
145 return *this;
146 }
147
148 inline QString key() const { return item.o->keyAt(item.index); }
149 inline QJsonValueRef value() const { return item; }
150 inline QJsonValueRef operator*() const { return item; }
151 inline QJsonValueRef *operator->() const { return &item; }
152 const QJsonValueRef operator[](qsizetype j) { return { item.o, qsizetype(item.index) + j }; }
153
154 inline bool operator==(const iterator &other) const
155 { return item.o == other.item.o && item.index == other.item.index; }
156 inline bool operator!=(const iterator &other) const { return !(*this == other); }
157 bool operator<(const iterator& other) const
158 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
159 bool operator<=(const iterator& other) const
160 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
161 bool operator>(const iterator& other) const { return !(*this <= other); }
162 bool operator>=(const iterator& other) const { return !(*this < other); }
163
164 inline iterator &operator++() { ++item.index; return *this; }
165 inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
166 inline iterator &operator--() { --item.index; return *this; }
167 inline iterator operator--(int) { iterator r = *this; --item.index; return r; }
168 inline iterator operator+(qsizetype j) const
169 { iterator r = *this; r.item.index += quint64(j); return r; }
170 inline iterator operator-(qsizetype j) const { return operator+(-j); }
171 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
172 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
173 qsizetype operator-(iterator j) const { return item.index - j.item.index; }
174
175 public:
176 inline bool operator==(const const_iterator &other) const
177 { return item.o == other.item.o && item.index == other.item.index; }
178 inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
179 bool operator<(const const_iterator& other) const
180 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
181 bool operator<=(const const_iterator& other) const
182 { Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
183 bool operator>(const const_iterator& other) const { return !(*this <= other); }
184 bool operator>=(const const_iterator& other) const { return !(*this < other); }
185 };
186 friend class iterator;
187
188 class const_iterator
189 {
190 friend class iterator;
191 QJsonValueRef item;
192
193 public:
194 typedef std::random_access_iterator_tag iterator_category;
195 typedef qsizetype difference_type;
196 typedef QJsonValue value_type;
197 typedef const QJsonValueRef reference;
198 typedef const QJsonValueRef *pointer;
199
200 inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
201 inline const_iterator(const QJsonObject *obj, qsizetype index)
202 : item(const_cast<QJsonObject*>(obj), index) { }
203 inline const_iterator(const iterator &other)
204 : item(other.item) { }
205
206 constexpr const_iterator(const const_iterator &other) = default;
207 const_iterator &operator=(const const_iterator &other)
208 {
209 item.o = other.item.o;
210 item.index = other.item.index;
211 return *this;
212 }
213
214 inline QString key() const { return item.o->keyAt(item.index); }
215 inline QJsonValueRef value() const { return item; }
216 inline const QJsonValueRef operator*() const { return item; }
217 inline const QJsonValueRef *operator->() const { return &item; }
218 const QJsonValueRef operator[](qsizetype j) { return { item.o, qsizetype(item.index) + j }; }
219
220 inline bool operator==(const const_iterator &other) const
221 { return item.o == other.item.o && item.index == other.item.index; }
222 inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
223 bool operator<(const const_iterator& other) const
224 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
225 bool operator<=(const const_iterator& other) const
226 { Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
227 bool operator>(const const_iterator& other) const { return !(*this <= other); }
228 bool operator>=(const const_iterator& other) const { return !(*this < other); }
229
230 inline const_iterator &operator++() { ++item.index; return *this; }
231 inline const_iterator operator++(int) { const_iterator r = *this; ++item.index; return r; }
232 inline const_iterator &operator--() { --item.index; return *this; }
233 inline const_iterator operator--(int) { const_iterator r = *this; --item.index; return r; }
234 inline const_iterator operator+(qsizetype j) const
235 { const_iterator r = *this; r.item.index += quint64(j); return r; }
236 inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
237 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
238 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
239 qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
240
241 inline bool operator==(const iterator &other) const
242 { return item.o == other.item.o && item.index == other.item.index; }
243 inline bool operator!=(const iterator &other) const { return !(*this == other); }
244 bool operator<(const iterator& other) const
245 { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
246 bool operator<=(const iterator& other) const
247 { Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
248 bool operator>(const iterator& other) const { return !(*this <= other); }
249 bool operator>=(const iterator& other) const { return !(*this < other); }
250 };
251 friend class const_iterator;
252
253 // STL style
254 inline iterator begin() { detach(); return iterator(this, 0); }
255 inline const_iterator begin() const { return const_iterator(this, 0); }
256 inline const_iterator constBegin() const { return const_iterator(this, 0); }
257 inline iterator end() { detach(); return iterator(this, size()); }
258 inline const_iterator end() const { return const_iterator(this, size()); }
259 inline const_iterator constEnd() const { return const_iterator(this, size()); }
260 iterator erase(iterator it);
261
262 // more Qt
263 typedef iterator Iterator;
264 typedef const_iterator ConstIterator;
265#if QT_STRINGVIEW_LEVEL < 2
266 iterator find(const QString &key);
267 const_iterator find(const QString &key) const { return constFind(key); }
268 const_iterator constFind(const QString &key) const;
269 iterator insert(const QString &key, const QJsonValue &value);
270#endif
271 iterator find(QStringView key);
272 iterator find(QLatin1String key);
273 const_iterator find(QStringView key) const { return constFind(key); }
274 const_iterator find(QLatin1String key) const { return constFind(key); }
275 const_iterator constFind(QStringView key) const;
276 const_iterator constFind(QLatin1String key) const;
277 iterator insert(QStringView key, const QJsonValue &value);
278 iterator insert(QLatin1String key, const QJsonValue &value);
279
280 // STL compatibility
281 typedef QJsonValue mapped_type;
282 typedef QString key_type;
283 typedef qsizetype size_type;
284
285 inline bool empty() const { return isEmpty(); }
286
287private:
288 friend class QJsonValue;
289 friend class QJsonDocument;
290 friend class QJsonValueRef;
291 friend class QCborMap;
292 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
293
294 QJsonObject(QCborContainerPrivate *object);
295 bool detach(qsizetype reserve = 0);
296
297 template <typename T> QJsonValue valueImpl(T key) const;
298 template <typename T> QJsonValueRef atImpl(T key);
299 template <typename T> void removeImpl(T key);
300 template <typename T> QJsonValue takeImpl(T key);
301 template <typename T> bool containsImpl(T key) const;
302 template <typename T> iterator findImpl(T key);
303 template <typename T> const_iterator constFindImpl(T key) const;
304 template <typename T> iterator insertImpl(T key, const QJsonValue &value);
305
306 QString keyAt(qsizetype i) const;
307 QJsonValue valueAt(qsizetype i) const;
308 void setValueAt(qsizetype i, const QJsonValue &val);
309 void removeAt(qsizetype i);
310 template <typename T> iterator insertAt(qsizetype i, T key, const QJsonValue &val, bool exists);
311
312 QExplicitlySharedDataPointer<QCborContainerPrivate> o;
313};
314
315Q_DECLARE_SHARED(QJsonObject)
316
317Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
318
319#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
320Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
321#endif
322
323#ifndef QT_NO_DATASTREAM
324Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
325Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
326#endif
327
328QT_END_NAMESPACE
329
330#endif // QJSONOBJECT_H
331