QtBase  v6.3.1
qjsonobject.h
Go to the documentation of this file.
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 
50 
51 class QDebug;
52 
54 
55 class Q_CORE_EXPORT QJsonObject
56 {
57 public:
59 
60  QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args);
61 
63 
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  o.swap(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
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);
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;
132 
133  inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
135 
136  constexpr iterator(const iterator &other) = default;
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; }
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 
185  {
186  friend class iterator;
188 
189  public:
190  typedef std::random_access_iterator_tag iterator_category;
193  typedef const QJsonValueRef reference;
194  typedef const QJsonValueRef *pointer;
195 
196  inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
198  : item(const_cast<QJsonObject*>(obj), index) { }
200  : item(other.item) { }
201 
202  constexpr const_iterator(const const_iterator &other) = default;
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; }
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()); }
257 
258  // more Qt
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
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;
275 
276  // STL compatibility
278  typedef QString key_type;
280 
281  inline bool empty() const { return isEmpty(); }
282 
283 private:
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 
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 
309 };
310 
312 
313 Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
314 
315 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
316 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
317 #endif
318 
319 #ifndef QT_NO_DATASTREAM
320 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
321 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
322 #endif
323 
325 
326 #endif // QJSONOBJECT_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
#define value
[5]
The QCborMap class is used to hold an associative container representable in CBOR.
Definition: qcbormap.h:57
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
template< typename Enum > size_t qHash(QFlags< Enum > flags, size_t seed=0) noexcept
The QJsonDocument class provides a way to read and write JSON documents.
Definition: qjsondocument.h:83
The QJsonObject::const_iterator class provides an STL-style const iterator for QJsonObject.
Definition: qjsonobject.h:185
bool operator==(const const_iterator &other) const
Definition: qjsonobject.h:216
const QJsonValueRef * pointer
Definition: qjsonobject.h:194
const_iterator & operator-=(qsizetype j)
Definition: qjsonobject.h:234
const QJsonValueRef reference
Definition: qjsonobject.h:193
const_iterator & operator--()
Definition: qjsonobject.h:228
const_iterator(const QJsonObject *obj, qsizetype index)
Definition: qjsonobject.h:197
constexpr const_iterator(const const_iterator &other)=default
const QJsonValueRef * operator->() const
Definition: qjsonobject.h:213
const_iterator operator--(int)
Definition: qjsonobject.h:229
const_iterator(const iterator &other)
Definition: qjsonobject.h:199
std::random_access_iterator_tag iterator_category
Definition: qjsonobject.h:190
const_iterator & operator=(const const_iterator &other)
Definition: qjsonobject.h:203
const_iterator operator-(qsizetype j) const
Definition: qjsonobject.h:232
const_iterator & operator++()
Definition: qjsonobject.h:226
QJsonValueRef value() const
Definition: qjsonobject.h:211
bool operator==(const iterator &other) const
Definition: qjsonobject.h:237
bool operator<(const const_iterator &other) const
Definition: qjsonobject.h:219
bool operator>(const iterator &other) const
Definition: qjsonobject.h:244
bool operator<=(const iterator &other) const
Definition: qjsonobject.h:242
bool operator<=(const const_iterator &other) const
Definition: qjsonobject.h:221
bool operator>=(const const_iterator &other) const
Definition: qjsonobject.h:224
const_iterator operator+(qsizetype j) const
Definition: qjsonobject.h:230
const_iterator operator++(int)
Definition: qjsonobject.h:227
const_iterator & operator+=(qsizetype j)
Definition: qjsonobject.h:233
qsizetype operator-(const_iterator j) const
Definition: qjsonobject.h:235
const QJsonValueRef operator*() const
Definition: qjsonobject.h:212
bool operator!=(const iterator &other) const
Definition: qjsonobject.h:239
bool operator<(const iterator &other) const
Definition: qjsonobject.h:240
bool operator>=(const iterator &other) const
Definition: qjsonobject.h:245
bool operator>(const const_iterator &other) const
Definition: qjsonobject.h:223
const QJsonValueRef operator[](qsizetype j)
Definition: qjsonobject.h:214
bool operator!=(const const_iterator &other) const
Definition: qjsonobject.h:218
The QJsonObject::iterator class provides an STL-style non-const iterator for QJsonObject.
Definition: qjsonobject.h:121
iterator operator--(int)
Definition: qjsonobject.h:163
bool operator<=(const const_iterator &other) const
Definition: qjsonobject.h:177
QJsonValueRef reference
Definition: qjsonobject.h:130
bool operator!=(const const_iterator &other) const
Definition: qjsonobject.h:174
bool operator<=(const iterator &other) const
Definition: qjsonobject.h:155
iterator operator-(qsizetype j) const
Definition: qjsonobject.h:166
qsizetype operator-(iterator j) const
Definition: qjsonobject.h:169
iterator & operator++()
Definition: qjsonobject.h:160
bool operator==(const const_iterator &other) const
Definition: qjsonobject.h:172
QJsonValueRef * pointer
Definition: qjsonobject.h:131
bool operator>=(const const_iterator &other) const
Definition: qjsonobject.h:180
QJsonValueRef value() const
Definition: qjsonobject.h:145
iterator operator++(int)
Definition: qjsonobject.h:161
bool operator>(const iterator &other) const
Definition: qjsonobject.h:157
bool operator<(const const_iterator &other) const
Definition: qjsonobject.h:175
QJsonValueRef * operator->() const
Definition: qjsonobject.h:147
iterator & operator-=(qsizetype j)
Definition: qjsonobject.h:168
iterator & operator=(const iterator &other)
Definition: qjsonobject.h:137
bool operator>(const const_iterator &other) const
Definition: qjsonobject.h:179
QString key() const
Definition: qjsonobject.h:144
qsizetype difference_type
Definition: qjsonobject.h:128
bool operator==(const iterator &other) const
Definition: qjsonobject.h:150
iterator & operator--()
Definition: qjsonobject.h:162
bool operator>=(const iterator &other) const
Definition: qjsonobject.h:158
const QJsonValueRef operator[](qsizetype j)
Definition: qjsonobject.h:148
iterator operator+(qsizetype j) const
Definition: qjsonobject.h:164
iterator & operator+=(qsizetype j)
Definition: qjsonobject.h:167
std::random_access_iterator_tag iterator_category
Definition: qjsonobject.h:127
constexpr iterator(const iterator &other)=default
QJsonValueRef operator*() const
Definition: qjsonobject.h:146
iterator(QJsonObject *obj, qsizetype index)
Definition: qjsonobject.h:134
bool operator!=(const iterator &other) const
Definition: qjsonobject.h:152
bool operator<(const iterator &other) const
Definition: qjsonobject.h:153
The QJsonObject class encapsulates a JSON object.
Definition: qjsonobject.h:56
const_iterator find(QStringView key) const
Definition: qjsonobject.h:269
iterator Iterator
Definition: qjsonobject.h:259
const_iterator constBegin() const
Definition: qjsonobject.h:252
iterator end()
Definition: qjsonobject.h:253
QJsonValue mapped_type
Definition: qjsonobject.h:277
const_iterator end() const
Definition: qjsonobject.h:254
bool empty() const
Definition: qjsonobject.h:281
void swap(QJsonObject &other) noexcept
Definition: qjsonobject.h:75
QString key_type
Definition: qjsonobject.h:278
qsizetype size_type
Definition: qjsonobject.h:279
const_iterator constEnd() const
Definition: qjsonobject.h:255
const_iterator find(const QString &key) const
Definition: qjsonobject.h:263
qsizetype count() const
Definition: qjsonobject.h:87
iterator begin()
Definition: qjsonobject.h:250
qsizetype length() const
Definition: qjsonobject.h:88
const_iterator begin() const
Definition: qjsonobject.h:251
const_iterator find(QLatin1String key) const
Definition: qjsonobject.h:270
const_iterator ConstIterator
Definition: qjsonobject.h:260
The QJsonValue class encapsulates a value in JSON.
Definition: qjsonvalue.h:60
The QJsonValueRef class is a helper class for QJsonValue.
Definition: qjsonvalue.h:154
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringList class provides a list of strings.
The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QStrin...
Definition: qstringview.h:122
map insert("Paris", "France")
QMap< QString, QString > map
[6]
set reserve(20000)
set contains("Julia")
GeneratorWrapper< T > take(size_t target, GeneratorWrapper< T > &&generator)
Definition: catch_p_p.h:4151
typename C::const_iterator const_iterator
typename C::iterator iterator
QString operator+(const ProString &one, const ProString &two)
Definition: proitems.cpp:281
qsizetype erase(QByteArray &ba, const T &t)
Definition: qbytearray.h:670
std::pair< T1, T2 > QPair
Definition: qcontainerfwd.h:56
constexpr bool operator!=(const timespec &t1, const timespec &t2)
Definition: qcore_unix_p.h:124
EGLOutputLayerEXT EGLint EGLAttrib value
unsigned long long quint64
Definition: qglobal.h:299
ptrdiff_t qsizetype
Definition: qglobal.h:308
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &)
bool operator==(const QMakeBaseKey &one, const QMakeBaseKey &two)
GLuint64 key
GLboolean r
[2]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLhandleARB obj
[2]
Definition: qopenglext.h:4164
GLuint GLfloat * val
Definition: qopenglext.h:1513
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
#define Q_DECLARE_SHARED(TYPE)
Definition: qtypeinfo.h:197
QStringList keys
settings remove("monkey")
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
this swap(other)
QGraphicsItem * item
QStringList::Iterator it
Definition: main.cpp:38
QDomElement find(const QString &tagName, const QDomElement &e)
Definition: main.cpp:39