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