Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/json/qjsonobject.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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 | #include <qjsonobject.h> | - | ||||||
41 | #include <qjsonvalue.h> | - | ||||||
42 | #include <qjsonarray.h> | - | ||||||
43 | #include <qstringlist.h> | - | ||||||
44 | #include <qdebug.h> | - | ||||||
45 | #include <qvariant.h> | - | ||||||
46 | #include "qjson_p.h" | - | ||||||
47 | #include "qjsonwriter_p.h" | - | ||||||
48 | - | |||||||
49 | QT_BEGIN_NAMESPACE | - | ||||||
50 | - | |||||||
51 | /*! | - | ||||||
52 | \class QJsonObject | - | ||||||
53 | \inmodule QtCore | - | ||||||
54 | \ingroup json | - | ||||||
55 | \ingroup shared | - | ||||||
56 | \reentrant | - | ||||||
57 | \since 5.0 | - | ||||||
58 | - | |||||||
59 | \brief The QJsonObject class encapsulates a JSON object. | - | ||||||
60 | - | |||||||
61 | A JSON object is a list of key value pairs, where the keys are unique strings | - | ||||||
62 | and the values are represented by a QJsonValue. | - | ||||||
63 | - | |||||||
64 | A QJsonObject can be converted to and from a QVariantMap. You can query the | - | ||||||
65 | number of (key, value) pairs with size(), insert(), and remove() entries from it | - | ||||||
66 | and iterate over its content using the standard C++ iterator pattern. | - | ||||||
67 | - | |||||||
68 | QJsonObject is an implicitly shared class, and shares the data with the document | - | ||||||
69 | it has been created from as long as it is not being modified. | - | ||||||
70 | - | |||||||
71 | You can convert the object to and from text based JSON through QJsonDocument. | - | ||||||
72 | - | |||||||
73 | \sa {JSON Support in Qt}, {JSON Save Game Example} | - | ||||||
74 | */ | - | ||||||
75 | - | |||||||
76 | /*! | - | ||||||
77 | \typedef QJsonObject::Iterator | - | ||||||
78 | - | |||||||
79 | Qt-style synonym for QJsonObject::iterator. | - | ||||||
80 | */ | - | ||||||
81 | - | |||||||
82 | /*! | - | ||||||
83 | \typedef QJsonObject::ConstIterator | - | ||||||
84 | - | |||||||
85 | Qt-style synonym for QJsonObject::const_iterator. | - | ||||||
86 | */ | - | ||||||
87 | - | |||||||
88 | /*! | - | ||||||
89 | \typedef QJsonObject::key_type | - | ||||||
90 | - | |||||||
91 | Typedef for QString. Provided for STL compatibility. | - | ||||||
92 | */ | - | ||||||
93 | - | |||||||
94 | /*! | - | ||||||
95 | \typedef QJsonObject::mapped_type | - | ||||||
96 | - | |||||||
97 | Typedef for QJsonValue. Provided for STL compatibility. | - | ||||||
98 | */ | - | ||||||
99 | - | |||||||
100 | /*! | - | ||||||
101 | \typedef QJsonObject::size_type | - | ||||||
102 | - | |||||||
103 | Typedef for int. Provided for STL compatibility. | - | ||||||
104 | */ | - | ||||||
105 | - | |||||||
106 | - | |||||||
107 | /*! | - | ||||||
108 | Constructs an empty JSON object. | - | ||||||
109 | - | |||||||
110 | \sa isEmpty() | - | ||||||
111 | */ | - | ||||||
112 | QJsonObject::QJsonObject() | - | ||||||
113 | : d(0), o(0) | - | ||||||
114 | { | - | ||||||
115 | } | - | ||||||
116 | - | |||||||
117 | /*! | - | ||||||
118 | \fn QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) | - | ||||||
119 | \since 5.4 | - | ||||||
120 | Constructs a QJsonObject instance initialized from \a args initialization list. | - | ||||||
121 | For example: | - | ||||||
122 | \code | - | ||||||
123 | QJsonObject object | - | ||||||
124 | { | - | ||||||
125 | {"property1", 1}, | - | ||||||
126 | {"property2", 2} | - | ||||||
127 | }; | - | ||||||
128 | \endcode | - | ||||||
129 | */ | - | ||||||
130 | - | |||||||
131 | /*! | - | ||||||
132 | \internal | - | ||||||
133 | */ | - | ||||||
134 | QJsonObject::QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object) | - | ||||||
135 | : d(data), o(object) | - | ||||||
136 | { | - | ||||||
137 | Q_ASSERT(d); | - | ||||||
138 | Q_ASSERT(o); | - | ||||||
139 | d->ref.ref(); | - | ||||||
140 | } | - | ||||||
141 | - | |||||||
142 | /*! | - | ||||||
143 | This method replaces part of the QJsonObject(std::initializer_list<QPair<QString, QJsonValue>> args) body. | - | ||||||
144 | The constructor needs to be inline, but we do not want to leak implementation details | - | ||||||
145 | of this class. | - | ||||||
146 | \note this method is called for an uninitialized object | - | ||||||
147 | \internal | - | ||||||
148 | */ | - | ||||||
149 | - | |||||||
150 | void QJsonObject::initialize() | - | ||||||
151 | { | - | ||||||
152 | d = 0; | - | ||||||
153 | o = 0; | - | ||||||
154 | } | - | ||||||
155 | - | |||||||
156 | /*! | - | ||||||
157 | Destroys the object. | - | ||||||
158 | */ | - | ||||||
159 | QJsonObject::~QJsonObject() | - | ||||||
160 | { | - | ||||||
161 | if (d && !d->ref.deref()) | - | ||||||
162 | delete d; | - | ||||||
163 | } | - | ||||||
164 | - | |||||||
165 | /*! | - | ||||||
166 | Creates a copy of \a other. | - | ||||||
167 | - | |||||||
168 | Since QJsonObject is implicitly shared, the copy is shallow | - | ||||||
169 | as long as the object does not get modified. | - | ||||||
170 | */ | - | ||||||
171 | QJsonObject::QJsonObject(const QJsonObject &other) | - | ||||||
172 | { | - | ||||||
173 | d = other.d; | - | ||||||
174 | o = other.o; | - | ||||||
175 | if (d) | - | ||||||
176 | d->ref.ref(); | - | ||||||
177 | } | - | ||||||
178 | - | |||||||
179 | /*! | - | ||||||
180 | Assigns \a other to this object. | - | ||||||
181 | */ | - | ||||||
182 | QJsonObject &QJsonObject::operator =(const QJsonObject &other) | - | ||||||
183 | { | - | ||||||
184 | if (d != other.d) { | - | ||||||
185 | if (d && !d->ref.deref()) | - | ||||||
186 | delete d; | - | ||||||
187 | d = other.d; | - | ||||||
188 | if (d) | - | ||||||
189 | d->ref.ref(); | - | ||||||
190 | } | - | ||||||
191 | o = other.o; | - | ||||||
192 | - | |||||||
193 | return *this; | - | ||||||
194 | } | - | ||||||
195 | - | |||||||
196 | /*! | - | ||||||
197 | Converts the variant map \a map to a QJsonObject. | - | ||||||
198 | - | |||||||
199 | The keys in \a map will be used as the keys in the JSON object, | - | ||||||
200 | and the QVariant values will be converted to JSON values. | - | ||||||
201 | - | |||||||
202 | \sa fromVariantHash(), toVariantMap(), QJsonValue::fromVariant() | - | ||||||
203 | */ | - | ||||||
204 | QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map) | - | ||||||
205 | { | - | ||||||
206 | QJsonObject object; | - | ||||||
207 | if (map.isEmpty()) | - | ||||||
208 | return object; | - | ||||||
209 | - | |||||||
210 | object.detach2(1024); | - | ||||||
211 | - | |||||||
212 | QVector<QJsonPrivate::offset> offsets; | - | ||||||
213 | QJsonPrivate::offset currentOffset; | - | ||||||
214 | currentOffset = sizeof(QJsonPrivate::Base); | - | ||||||
215 | - | |||||||
216 | // the map is already sorted, so we can simply append one entry after the other and | - | ||||||
217 | // write the offset table at the end | - | ||||||
218 | for (QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) { | - | ||||||
219 | QString key = it.key(); | - | ||||||
220 | QJsonValue val = QJsonValue::fromVariant(it.value()); | - | ||||||
221 | - | |||||||
222 | bool latinOrIntValue; | - | ||||||
223 | int valueSize = QJsonPrivate::Value::requiredStorage(val, &latinOrIntValue); | - | ||||||
224 | - | |||||||
225 | bool latinKey = QJsonPrivate::useCompressed(key); | - | ||||||
226 | int valueOffset = sizeof(QJsonPrivate::Entry) + QJsonPrivate::qStringSize(key, latinKey); | - | ||||||
227 | int requiredSize = valueOffset + valueSize; | - | ||||||
228 | - | |||||||
229 | if (!object.detach2(requiredSize + sizeof(QJsonPrivate::offset))) // offset for the new index entry | - | ||||||
230 | return QJsonObject(); | - | ||||||
231 | - | |||||||
232 | QJsonPrivate::Entry *e = reinterpret_cast<QJsonPrivate::Entry *>(reinterpret_cast<char *>(object.o) + currentOffset); | - | ||||||
233 | e->value.type = val.t; | - | ||||||
234 | e->value.latinKey = latinKey; | - | ||||||
235 | e->value.latinOrIntValue = latinOrIntValue; | - | ||||||
236 | e->value.value = QJsonPrivate::Value::valueToStore(val, (char *)e - (char *)object.o + valueOffset); | - | ||||||
237 | QJsonPrivate::copyString((char *)(e + 1), key, latinKey); | - | ||||||
238 | if (valueSize) | - | ||||||
239 | QJsonPrivate::Value::copyData(val, (char *)e + valueOffset, latinOrIntValue); | - | ||||||
240 | - | |||||||
241 | offsets << currentOffset; | - | ||||||
242 | currentOffset += requiredSize; | - | ||||||
243 | object.o->size = currentOffset; | - | ||||||
244 | } | - | ||||||
245 | - | |||||||
246 | // write table | - | ||||||
247 | object.o->tableOffset = currentOffset; | - | ||||||
248 | if (!object.detach2(sizeof(QJsonPrivate::offset)*offsets.size())) | - | ||||||
249 | return QJsonObject(); | - | ||||||
250 | memcpy(object.o->table(), offsets.constData(), offsets.size()*sizeof(uint)); | - | ||||||
251 | object.o->length = offsets.size(); | - | ||||||
252 | object.o->size = currentOffset + sizeof(QJsonPrivate::offset)*offsets.size(); | - | ||||||
253 | - | |||||||
254 | return object; | - | ||||||
255 | } | - | ||||||
256 | - | |||||||
257 | /*! | - | ||||||
258 | Converts this object to a QVariantMap. | - | ||||||
259 | - | |||||||
260 | Returns the created map. | - | ||||||
261 | - | |||||||
262 | \sa toVariantHash() | - | ||||||
263 | */ | - | ||||||
264 | QVariantMap QJsonObject::toVariantMap() const | - | ||||||
265 | { | - | ||||||
266 | QVariantMap map; | - | ||||||
267 | if (o) { | - | ||||||
268 | for (uint i = 0; i < o->length; ++i) { | - | ||||||
269 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
270 | map.insert(e->key(), QJsonValue(d, o, e->value).toVariant()); | - | ||||||
271 | } | - | ||||||
272 | } | - | ||||||
273 | return map; | - | ||||||
274 | } | - | ||||||
275 | - | |||||||
276 | /*! | - | ||||||
277 | Converts the variant hash \a hash to a QJsonObject. | - | ||||||
278 | \since 5.5 | - | ||||||
279 | - | |||||||
280 | The keys in \a hash will be used as the keys in the JSON object, | - | ||||||
281 | and the QVariant values will be converted to JSON values. | - | ||||||
282 | - | |||||||
283 | \sa fromVariantMap(), toVariantHash(), QJsonValue::fromVariant() | - | ||||||
284 | */ | - | ||||||
285 | QJsonObject QJsonObject::fromVariantHash(const QVariantHash &hash) | - | ||||||
286 | { | - | ||||||
287 | // ### this is implemented the trivial way, not the most efficient way | - | ||||||
288 | - | |||||||
289 | QJsonObject object; | - | ||||||
290 | for (QVariantHash::const_iterator it = hash.constBegin(); it != hash.constEnd(); ++it) | - | ||||||
291 | object.insert(it.key(), QJsonValue::fromVariant(it.value())); | - | ||||||
292 | return object; | - | ||||||
293 | } | - | ||||||
294 | - | |||||||
295 | /*! | - | ||||||
296 | Converts this object to a QVariantHash. | - | ||||||
297 | \since 5.5 | - | ||||||
298 | - | |||||||
299 | Returns the created hash. | - | ||||||
300 | - | |||||||
301 | \sa toVariantMap() | - | ||||||
302 | */ | - | ||||||
303 | QVariantHash QJsonObject::toVariantHash() const | - | ||||||
304 | { | - | ||||||
305 | QVariantHash hash; | - | ||||||
306 | if (o) {
| 0 | ||||||
307 | hash.reserve(o->length); | - | ||||||
308 | for (uint i = 0; i < o->length; ++i) {
| 0 | ||||||
309 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
310 | hash.insert(e->key(), QJsonValue(d, o, e->value).toVariant()); | - | ||||||
311 | } never executed: end of block | 0 | ||||||
312 | } never executed: end of block | 0 | ||||||
313 | return hash; never executed: return hash; | 0 | ||||||
314 | } | - | ||||||
315 | - | |||||||
316 | /*! | - | ||||||
317 | Returns a list of all keys in this object. | - | ||||||
318 | - | |||||||
319 | The list is sorted lexographically. | - | ||||||
320 | */ | - | ||||||
321 | QStringList QJsonObject::keys() const | - | ||||||
322 | { | - | ||||||
323 | QStringList keys; | - | ||||||
324 | if (o) { | - | ||||||
325 | keys.reserve(o->length); | - | ||||||
326 | for (uint i = 0; i < o->length; ++i) { | - | ||||||
327 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
328 | keys.append(e->key()); | - | ||||||
329 | } | - | ||||||
330 | } | - | ||||||
331 | return keys; | - | ||||||
332 | } | - | ||||||
333 | - | |||||||
334 | /*! | - | ||||||
335 | Returns the number of (key, value) pairs stored in the object. | - | ||||||
336 | */ | - | ||||||
337 | int QJsonObject::size() const | - | ||||||
338 | { | - | ||||||
339 | if (!d) | - | ||||||
340 | return 0; | - | ||||||
341 | - | |||||||
342 | return o->length; | - | ||||||
343 | } | - | ||||||
344 | - | |||||||
345 | /*! | - | ||||||
346 | Returns \c true if the object is empty. This is the same as size() == 0. | - | ||||||
347 | - | |||||||
348 | \sa size() | - | ||||||
349 | */ | - | ||||||
350 | bool QJsonObject::isEmpty() const | - | ||||||
351 | { | - | ||||||
352 | if (!d) | - | ||||||
353 | return true; | - | ||||||
354 | - | |||||||
355 | return !o->length; | - | ||||||
356 | } | - | ||||||
357 | - | |||||||
358 | /*! | - | ||||||
359 | Returns a QJsonValue representing the value for the key \a key. | - | ||||||
360 | - | |||||||
361 | The returned QJsonValue is QJsonValue::Undefined if the key does not exist. | - | ||||||
362 | - | |||||||
363 | \sa QJsonValue, QJsonValue::isUndefined() | - | ||||||
364 | */ | - | ||||||
365 | QJsonValue QJsonObject::value(const QString &key) const | - | ||||||
366 | { | - | ||||||
367 | if (!d) | - | ||||||
368 | return QJsonValue(QJsonValue::Undefined); | - | ||||||
369 | - | |||||||
370 | bool keyExists; | - | ||||||
371 | int i = o->indexOf(key, &keyExists); | - | ||||||
372 | if (!keyExists) | - | ||||||
373 | return QJsonValue(QJsonValue::Undefined); | - | ||||||
374 | return QJsonValue(d, o, o->entryAt(i)->value); | - | ||||||
375 | } | - | ||||||
376 | - | |||||||
377 | /*! | - | ||||||
378 | \overload | - | ||||||
379 | \since 5.7 | - | ||||||
380 | */ | - | ||||||
381 | QJsonValue QJsonObject::value(QLatin1String key) const | - | ||||||
382 | { | - | ||||||
383 | if (!d)
| 0-86427 | ||||||
384 | return QJsonValue(QJsonValue::Undefined); never executed: return QJsonValue(QJsonValue::Undefined); | 0 | ||||||
385 | - | |||||||
386 | bool keyExists; | - | ||||||
387 | int i = o->indexOf(key, &keyExists); | - | ||||||
388 | if (!keyExists)
| 147-86280 | ||||||
389 | return QJsonValue(QJsonValue::Undefined); executed 147 times by 2 tests: return QJsonValue(QJsonValue::Undefined); Executed by:
| 147 | ||||||
390 | return QJsonValue(d, o, o->entryAt(i)->value); executed 86280 times by 115 tests: return QJsonValue(d, o, o->entryAt(i)->value); Executed by:
| 86280 | ||||||
391 | } | - | ||||||
392 | - | |||||||
393 | /*! | - | ||||||
394 | Returns a QJsonValue representing the value for the key \a key. | - | ||||||
395 | - | |||||||
396 | This does the same as value(). | - | ||||||
397 | - | |||||||
398 | The returned QJsonValue is QJsonValue::Undefined if the key does not exist. | - | ||||||
399 | - | |||||||
400 | \sa value(), QJsonValue, QJsonValue::isUndefined() | - | ||||||
401 | */ | - | ||||||
402 | QJsonValue QJsonObject::operator [](const QString &key) const | - | ||||||
403 | { | - | ||||||
404 | return value(key); | - | ||||||
405 | } | - | ||||||
406 | - | |||||||
407 | /*! | - | ||||||
408 | \fn QJsonValue QJsonObject::operator [](QLatin1String key) const | - | ||||||
409 | - | |||||||
410 | \overload | - | ||||||
411 | \since 5.7 | - | ||||||
412 | */ | - | ||||||
413 | - | |||||||
414 | /*! | - | ||||||
415 | Returns a reference to the value for \a key. | - | ||||||
416 | - | |||||||
417 | The return value is of type QJsonValueRef, a helper class for QJsonArray | - | ||||||
418 | and QJsonObject. When you get an object of type QJsonValueRef, you can | - | ||||||
419 | use it as if it were a reference to a QJsonValue. If you assign to it, | - | ||||||
420 | the assignment will apply to the element in the QJsonArray or QJsonObject | - | ||||||
421 | from which you got the reference. | - | ||||||
422 | - | |||||||
423 | \sa value() | - | ||||||
424 | */ | - | ||||||
425 | QJsonValueRef QJsonObject::operator [](const QString &key) | - | ||||||
426 | { | - | ||||||
427 | // ### somewhat inefficient, as we lookup the key twice if it doesn't yet exist | - | ||||||
428 | bool keyExists = false; | - | ||||||
429 | int index = o ? o->indexOf(key, &keyExists) : -1; | - | ||||||
430 | if (!keyExists) { | - | ||||||
431 | iterator i = insert(key, QJsonValue()); | - | ||||||
432 | index = i.i; | - | ||||||
433 | } | - | ||||||
434 | return QJsonValueRef(this, index); | - | ||||||
435 | } | - | ||||||
436 | - | |||||||
437 | /*! | - | ||||||
438 | \overload | - | ||||||
439 | \since 5.7 | - | ||||||
440 | */ | - | ||||||
441 | QJsonValueRef QJsonObject::operator [](QLatin1String key) | - | ||||||
442 | { | - | ||||||
443 | // ### optimize me | - | ||||||
444 | never executed: return operator[](QString(key));return operator[](QString(key)); never executed: return operator[](QString(key)); | 0 | ||||||
445 | } | - | ||||||
446 | - | |||||||
447 | /*! | - | ||||||
448 | Inserts a new item with the key \a key and a value of \a value. | - | ||||||
449 | - | |||||||
450 | If there is already an item with the key \a key, then that item's value | - | ||||||
451 | is replaced with \a value. | - | ||||||
452 | - | |||||||
453 | Returns an iterator pointing to the inserted item. | - | ||||||
454 | - | |||||||
455 | If the value is QJsonValue::Undefined, it will cause the key to get removed | - | ||||||
456 | from the object. The returned iterator will then point to end(). | - | ||||||
457 | - | |||||||
458 | \sa remove(), take(), QJsonObject::iterator, end() | - | ||||||
459 | */ | - | ||||||
460 | QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &value) | - | ||||||
461 | { | - | ||||||
462 | if (value.t == QJsonValue::Undefined) { | - | ||||||
463 | remove(key); | - | ||||||
464 | return end(); | - | ||||||
465 | } | - | ||||||
466 | QJsonValue val = value; | - | ||||||
467 | - | |||||||
468 | bool latinOrIntValue; | - | ||||||
469 | int valueSize = QJsonPrivate::Value::requiredStorage(val, &latinOrIntValue); | - | ||||||
470 | - | |||||||
471 | bool latinKey = QJsonPrivate::useCompressed(key); | - | ||||||
472 | int valueOffset = sizeof(QJsonPrivate::Entry) + QJsonPrivate::qStringSize(key, latinKey); | - | ||||||
473 | int requiredSize = valueOffset + valueSize; | - | ||||||
474 | - | |||||||
475 | if (!detach2(requiredSize + sizeof(QJsonPrivate::offset))) // offset for the new index entry | - | ||||||
476 | return iterator(); | - | ||||||
477 | - | |||||||
478 | if (!o->length) | - | ||||||
479 | o->tableOffset = sizeof(QJsonPrivate::Object); | - | ||||||
480 | - | |||||||
481 | bool keyExists = false; | - | ||||||
482 | int pos = o->indexOf(key, &keyExists); | - | ||||||
483 | if (keyExists) | - | ||||||
484 | ++d->compactionCounter; | - | ||||||
485 | - | |||||||
486 | uint off = o->reserveSpace(requiredSize, pos, 1, keyExists); | - | ||||||
487 | if (!off) | - | ||||||
488 | return end(); | - | ||||||
489 | - | |||||||
490 | QJsonPrivate::Entry *e = o->entryAt(pos); | - | ||||||
491 | e->value.type = val.t; | - | ||||||
492 | e->value.latinKey = latinKey; | - | ||||||
493 | e->value.latinOrIntValue = latinOrIntValue; | - | ||||||
494 | e->value.value = QJsonPrivate::Value::valueToStore(val, (char *)e - (char *)o + valueOffset); | - | ||||||
495 | QJsonPrivate::copyString((char *)(e + 1), key, latinKey); | - | ||||||
496 | if (valueSize) | - | ||||||
497 | QJsonPrivate::Value::copyData(val, (char *)e + valueOffset, latinOrIntValue); | - | ||||||
498 | - | |||||||
499 | if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) | - | ||||||
500 | compact(); | - | ||||||
501 | - | |||||||
502 | return iterator(this, pos); | - | ||||||
503 | } | - | ||||||
504 | - | |||||||
505 | /*! | - | ||||||
506 | Removes \a key from the object. | - | ||||||
507 | - | |||||||
508 | \sa insert(), take() | - | ||||||
509 | */ | - | ||||||
510 | void QJsonObject::remove(const QString &key) | - | ||||||
511 | { | - | ||||||
512 | if (!d) | - | ||||||
513 | return; | - | ||||||
514 | - | |||||||
515 | bool keyExists; | - | ||||||
516 | int index = o->indexOf(key, &keyExists); | - | ||||||
517 | if (!keyExists) | - | ||||||
518 | return; | - | ||||||
519 | - | |||||||
520 | detach2(); | - | ||||||
521 | o->removeItems(index, 1); | - | ||||||
522 | ++d->compactionCounter; | - | ||||||
523 | if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) | - | ||||||
524 | compact(); | - | ||||||
525 | } | - | ||||||
526 | - | |||||||
527 | /*! | - | ||||||
528 | Removes \a key from the object. | - | ||||||
529 | - | |||||||
530 | Returns a QJsonValue containing the value referenced by \a key. | - | ||||||
531 | If \a key was not contained in the object, the returned QJsonValue | - | ||||||
532 | is QJsonValue::Undefined. | - | ||||||
533 | - | |||||||
534 | \sa insert(), remove(), QJsonValue | - | ||||||
535 | */ | - | ||||||
536 | QJsonValue QJsonObject::take(const QString &key) | - | ||||||
537 | { | - | ||||||
538 | if (!o) | - | ||||||
539 | return QJsonValue(QJsonValue::Undefined); | - | ||||||
540 | - | |||||||
541 | bool keyExists; | - | ||||||
542 | int index = o->indexOf(key, &keyExists); | - | ||||||
543 | if (!keyExists) | - | ||||||
544 | return QJsonValue(QJsonValue::Undefined); | - | ||||||
545 | - | |||||||
546 | QJsonValue v(d, o, o->entryAt(index)->value); | - | ||||||
547 | detach2(); | - | ||||||
548 | o->removeItems(index, 1); | - | ||||||
549 | ++d->compactionCounter; | - | ||||||
550 | if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) | - | ||||||
551 | compact(); | - | ||||||
552 | - | |||||||
553 | return v; | - | ||||||
554 | } | - | ||||||
555 | - | |||||||
556 | /*! | - | ||||||
557 | Returns \c true if the object contains key \a key. | - | ||||||
558 | - | |||||||
559 | \sa insert(), remove(), take() | - | ||||||
560 | */ | - | ||||||
561 | bool QJsonObject::contains(const QString &key) const | - | ||||||
562 | { | - | ||||||
563 | if (!o) | - | ||||||
564 | return false; | - | ||||||
565 | - | |||||||
566 | bool keyExists; | - | ||||||
567 | o->indexOf(key, &keyExists); | - | ||||||
568 | return keyExists; | - | ||||||
569 | } | - | ||||||
570 | - | |||||||
571 | /*! | - | ||||||
572 | \overload | - | ||||||
573 | \since 5.7 | - | ||||||
574 | */ | - | ||||||
575 | bool QJsonObject::contains(QLatin1String key) const | - | ||||||
576 | { | - | ||||||
577 | if (!o)
| 0-12 | ||||||
578 | return false; never executed: return false; | 0 | ||||||
579 | - | |||||||
580 | bool keyExists; | - | ||||||
581 | o->indexOf(key, &keyExists); | - | ||||||
582 | return keyExists; executed 12 times by 1 test: return keyExists; Executed by:
| 12 | ||||||
583 | } | - | ||||||
584 | - | |||||||
585 | /*! | - | ||||||
586 | Returns \c true if \a other is equal to this object. | - | ||||||
587 | */ | - | ||||||
588 | bool QJsonObject::operator==(const QJsonObject &other) const | - | ||||||
589 | { | - | ||||||
590 | if (o == other.o) | - | ||||||
591 | return true; | - | ||||||
592 | - | |||||||
593 | if (!o) | - | ||||||
594 | return !other.o->length; | - | ||||||
595 | if (!other.o) | - | ||||||
596 | return !o->length; | - | ||||||
597 | if (o->length != other.o->length) | - | ||||||
598 | return false; | - | ||||||
599 | - | |||||||
600 | for (uint i = 0; i < o->length; ++i) { | - | ||||||
601 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
602 | QJsonValue v(d, o, e->value); | - | ||||||
603 | if (other.value(e->key()) != v) | - | ||||||
604 | return false; | - | ||||||
605 | } | - | ||||||
606 | - | |||||||
607 | return true; | - | ||||||
608 | } | - | ||||||
609 | - | |||||||
610 | /*! | - | ||||||
611 | Returns \c true if \a other is not equal to this object. | - | ||||||
612 | */ | - | ||||||
613 | bool QJsonObject::operator!=(const QJsonObject &other) const | - | ||||||
614 | { | - | ||||||
615 | return !(*this == other); | - | ||||||
616 | } | - | ||||||
617 | - | |||||||
618 | /*! | - | ||||||
619 | Removes the (key, value) pair pointed to by the iterator \a it | - | ||||||
620 | from the map, and returns an iterator to the next item in the | - | ||||||
621 | map. | - | ||||||
622 | - | |||||||
623 | \sa remove() | - | ||||||
624 | */ | - | ||||||
625 | QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it) | - | ||||||
626 | { | - | ||||||
627 | Q_ASSERT(d && d->ref.load() == 1); | - | ||||||
628 | if (it.o != this || it.i < 0 || it.i >= (int)o->length) | - | ||||||
629 | return iterator(this, o->length); | - | ||||||
630 | - | |||||||
631 | int index = it.i; | - | ||||||
632 | - | |||||||
633 | o->removeItems(index, 1); | - | ||||||
634 | ++d->compactionCounter; | - | ||||||
635 | if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) | - | ||||||
636 | compact(); | - | ||||||
637 | - | |||||||
638 | // iterator hasn't changed | - | ||||||
639 | return it; | - | ||||||
640 | } | - | ||||||
641 | - | |||||||
642 | /*! | - | ||||||
643 | Returns an iterator pointing to the item with key \a key in the | - | ||||||
644 | map. | - | ||||||
645 | - | |||||||
646 | If the map contains no item with key \a key, the function | - | ||||||
647 | returns end(). | - | ||||||
648 | */ | - | ||||||
649 | QJsonObject::iterator QJsonObject::find(const QString &key) | - | ||||||
650 | { | - | ||||||
651 | bool keyExists = false; | - | ||||||
652 | int index = o ? o->indexOf(key, &keyExists) : 0; | - | ||||||
653 | if (!keyExists) | - | ||||||
654 | return end(); | - | ||||||
655 | detach2(); | - | ||||||
656 | return iterator(this, index); | - | ||||||
657 | } | - | ||||||
658 | - | |||||||
659 | /*! | - | ||||||
660 | \overload | - | ||||||
661 | \since 5.7 | - | ||||||
662 | */ | - | ||||||
663 | QJsonObject::iterator QJsonObject::find(QLatin1String key) | - | ||||||
664 | { | - | ||||||
665 | bool keyExists = false; | - | ||||||
666 | int index = o ? o->indexOf(key, &keyExists) : 0;
| 0 | ||||||
667 | if (!keyExists)
| 0 | ||||||
668 | return end(); never executed: return end(); | 0 | ||||||
669 | detach2(); | - | ||||||
670 | return iterator(this, index); never executed: return iterator(this, index); | 0 | ||||||
671 | } | - | ||||||
672 | - | |||||||
673 | /*! \fn QJsonObject::const_iterator QJsonObject::find(const QString &key) const | - | ||||||
674 | - | |||||||
675 | \overload | - | ||||||
676 | */ | - | ||||||
677 | - | |||||||
678 | /*! \fn QJsonObject::const_iterator QJsonObject::find(QLatin1String key) const | - | ||||||
679 | - | |||||||
680 | \overload | - | ||||||
681 | \since 5.7 | - | ||||||
682 | */ | - | ||||||
683 | - | |||||||
684 | /*! | - | ||||||
685 | Returns a const iterator pointing to the item with key \a key in the | - | ||||||
686 | map. | - | ||||||
687 | - | |||||||
688 | If the map contains no item with key \a key, the function | - | ||||||
689 | returns constEnd(). | - | ||||||
690 | */ | - | ||||||
691 | QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const | - | ||||||
692 | { | - | ||||||
693 | bool keyExists = false; | - | ||||||
694 | int index = o ? o->indexOf(key, &keyExists) : 0; | - | ||||||
695 | if (!keyExists) | - | ||||||
696 | return end(); | - | ||||||
697 | return const_iterator(this, index); | - | ||||||
698 | } | - | ||||||
699 | - | |||||||
700 | /*! | - | ||||||
701 | \overload | - | ||||||
702 | \since 5.7 | - | ||||||
703 | */ | - | ||||||
704 | QJsonObject::const_iterator QJsonObject::constFind(QLatin1String key) const | - | ||||||
705 | { | - | ||||||
706 | bool keyExists = false; | - | ||||||
707 | int index = o ? o->indexOf(key, &keyExists) : 0;
| 0 | ||||||
708 | if (!keyExists)
| 0 | ||||||
709 | return end(); never executed: return end(); | 0 | ||||||
710 | return const_iterator(this, index); never executed: return const_iterator(this, index); | 0 | ||||||
711 | } | - | ||||||
712 | - | |||||||
713 | /*! \fn int QJsonObject::count() const | - | ||||||
714 | - | |||||||
715 | \overload | - | ||||||
716 | - | |||||||
717 | Same as size(). | - | ||||||
718 | */ | - | ||||||
719 | - | |||||||
720 | /*! \fn int QJsonObject::length() const | - | ||||||
721 | - | |||||||
722 | \overload | - | ||||||
723 | - | |||||||
724 | Same as size(). | - | ||||||
725 | */ | - | ||||||
726 | - | |||||||
727 | /*! \fn QJsonObject::iterator QJsonObject::begin() | - | ||||||
728 | - | |||||||
729 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in | - | ||||||
730 | the object. | - | ||||||
731 | - | |||||||
732 | \sa constBegin(), end() | - | ||||||
733 | */ | - | ||||||
734 | - | |||||||
735 | /*! \fn QJsonObject::const_iterator QJsonObject::begin() const | - | ||||||
736 | - | |||||||
737 | \overload | - | ||||||
738 | */ | - | ||||||
739 | - | |||||||
740 | /*! \fn QJsonObject::const_iterator QJsonObject::constBegin() const | - | ||||||
741 | - | |||||||
742 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item | - | ||||||
743 | in the object. | - | ||||||
744 | - | |||||||
745 | \sa begin(), constEnd() | - | ||||||
746 | */ | - | ||||||
747 | - | |||||||
748 | /*! \fn QJsonObject::iterator QJsonObject::end() | - | ||||||
749 | - | |||||||
750 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item | - | ||||||
751 | after the last item in the object. | - | ||||||
752 | - | |||||||
753 | \sa begin(), constEnd() | - | ||||||
754 | */ | - | ||||||
755 | - | |||||||
756 | /*! \fn QJsonObject::const_iterator QJsonObject::end() const | - | ||||||
757 | - | |||||||
758 | \overload | - | ||||||
759 | */ | - | ||||||
760 | - | |||||||
761 | /*! \fn QJsonObject::const_iterator QJsonObject::constEnd() const | - | ||||||
762 | - | |||||||
763 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary | - | ||||||
764 | item after the last item in the object. | - | ||||||
765 | - | |||||||
766 | \sa constBegin(), end() | - | ||||||
767 | */ | - | ||||||
768 | - | |||||||
769 | /*! | - | ||||||
770 | \fn bool QJsonObject::empty() const | - | ||||||
771 | - | |||||||
772 | This function is provided for STL compatibility. It is equivalent | - | ||||||
773 | to isEmpty(), returning \c true if the object is empty; otherwise | - | ||||||
774 | returning \c false. | - | ||||||
775 | */ | - | ||||||
776 | - | |||||||
777 | /*! \class QJsonObject::iterator | - | ||||||
778 | \inmodule QtCore | - | ||||||
779 | \ingroup json | - | ||||||
780 | \reentrant | - | ||||||
781 | \since 5.0 | - | ||||||
782 | - | |||||||
783 | \brief The QJsonObject::iterator class provides an STL-style non-const iterator for QJsonObject. | - | ||||||
784 | - | |||||||
785 | QJsonObject::iterator allows you to iterate over a QJsonObject | - | ||||||
786 | and to modify the value (but not the key) stored under | - | ||||||
787 | a particular key. If you want to iterate over a const QJsonObject, you | - | ||||||
788 | should use QJsonObject::const_iterator. It is generally good practice to | - | ||||||
789 | use QJsonObject::const_iterator on a non-const QJsonObject as well, unless you | - | ||||||
790 | need to change the QJsonObject through the iterator. Const iterators are | - | ||||||
791 | slightly faster, and improve code readability. | - | ||||||
792 | - | |||||||
793 | The default QJsonObject::iterator constructor creates an uninitialized | - | ||||||
794 | iterator. You must initialize it using a QJsonObject function like | - | ||||||
795 | QJsonObject::begin(), QJsonObject::end(), or QJsonObject::find() before you can | - | ||||||
796 | start iterating. | - | ||||||
797 | - | |||||||
798 | Multiple iterators can be used on the same object. Existing iterators will however | - | ||||||
799 | become dangling once the object gets modified. | - | ||||||
800 | - | |||||||
801 | \sa QJsonObject::const_iterator, {JSON Support in Qt}, {JSON Save Game Example} | - | ||||||
802 | */ | - | ||||||
803 | - | |||||||
804 | /*! \typedef QJsonObject::iterator::difference_type | - | ||||||
805 | - | |||||||
806 | \internal | - | ||||||
807 | */ | - | ||||||
808 | - | |||||||
809 | /*! \typedef QJsonObject::iterator::iterator_category | - | ||||||
810 | - | |||||||
811 | A synonym for \e {std::random_access_iterator_tag} indicating | - | ||||||
812 | this iterator is a random-access iterator. | - | ||||||
813 | - | |||||||
814 | \note In Qt versions before 5.6, this was set by mistake to | - | ||||||
815 | \e {std::bidirectional_iterator_tag}. | - | ||||||
816 | */ | - | ||||||
817 | - | |||||||
818 | /*! \typedef QJsonObject::iterator::reference | - | ||||||
819 | - | |||||||
820 | \internal | - | ||||||
821 | */ | - | ||||||
822 | - | |||||||
823 | /*! \typedef QJsonObject::iterator::value_type | - | ||||||
824 | - | |||||||
825 | \internal | - | ||||||
826 | */ | - | ||||||
827 | - | |||||||
828 | /*! \typedef QJsonObject::iterator::pointer | - | ||||||
829 | - | |||||||
830 | \internal | - | ||||||
831 | */ | - | ||||||
832 | - | |||||||
833 | /*! \fn QJsonObject::iterator::iterator() | - | ||||||
834 | - | |||||||
835 | Constructs an uninitialized iterator. | - | ||||||
836 | - | |||||||
837 | Functions like key(), value(), and operator++() must not be | - | ||||||
838 | called on an uninitialized iterator. Use operator=() to assign a | - | ||||||
839 | value to it before using it. | - | ||||||
840 | - | |||||||
841 | \sa QJsonObject::begin(), QJsonObject::end() | - | ||||||
842 | */ | - | ||||||
843 | - | |||||||
844 | /*! \fn QJsonObject::iterator::iterator(QJsonObject *obj, int index) | - | ||||||
845 | \internal | - | ||||||
846 | */ | - | ||||||
847 | - | |||||||
848 | /*! \fn QString QJsonObject::iterator::key() const | - | ||||||
849 | - | |||||||
850 | Returns the current item's key. | - | ||||||
851 | - | |||||||
852 | There is no direct way of changing an item's key through an | - | ||||||
853 | iterator, although it can be done by calling QJsonObject::erase() | - | ||||||
854 | followed by QJsonObject::insert(). | - | ||||||
855 | - | |||||||
856 | \sa value() | - | ||||||
857 | */ | - | ||||||
858 | - | |||||||
859 | /*! \fn QJsonValueRef QJsonObject::iterator::value() const | - | ||||||
860 | - | |||||||
861 | Returns a modifiable reference to the current item's value. | - | ||||||
862 | - | |||||||
863 | You can change the value of an item by using value() on | - | ||||||
864 | the left side of an assignment. | - | ||||||
865 | - | |||||||
866 | The return value is of type QJsonValueRef, a helper class for QJsonArray | - | ||||||
867 | and QJsonObject. When you get an object of type QJsonValueRef, you can | - | ||||||
868 | use it as if it were a reference to a QJsonValue. If you assign to it, | - | ||||||
869 | the assignment will apply to the element in the QJsonArray or QJsonObject | - | ||||||
870 | from which you got the reference. | - | ||||||
871 | - | |||||||
872 | \sa key(), operator*() | - | ||||||
873 | */ | - | ||||||
874 | - | |||||||
875 | /*! \fn QJsonValueRef QJsonObject::iterator::operator*() const | - | ||||||
876 | - | |||||||
877 | Returns a modifiable reference to the current item's value. | - | ||||||
878 | - | |||||||
879 | Same as value(). | - | ||||||
880 | - | |||||||
881 | The return value is of type QJsonValueRef, a helper class for QJsonArray | - | ||||||
882 | and QJsonObject. When you get an object of type QJsonValueRef, you can | - | ||||||
883 | use it as if it were a reference to a QJsonValue. If you assign to it, | - | ||||||
884 | the assignment will apply to the element in the QJsonArray or QJsonObject | - | ||||||
885 | from which you got the reference. | - | ||||||
886 | - | |||||||
887 | \sa key() | - | ||||||
888 | */ | - | ||||||
889 | - | |||||||
890 | /*! \fn QJsonValueRef *QJsonObject::iterator::operator->() const | - | ||||||
891 | - | |||||||
892 | Returns a pointer to a modifiable reference to the current item. | - | ||||||
893 | */ | - | ||||||
894 | - | |||||||
895 | /*! | - | ||||||
896 | \fn bool QJsonObject::iterator::operator==(const iterator &other) const | - | ||||||
897 | \fn bool QJsonObject::iterator::operator==(const const_iterator &other) const | - | ||||||
898 | - | |||||||
899 | Returns \c true if \a other points to the same item as this | - | ||||||
900 | iterator; otherwise returns \c false. | - | ||||||
901 | - | |||||||
902 | \sa operator!=() | - | ||||||
903 | */ | - | ||||||
904 | - | |||||||
905 | /*! | - | ||||||
906 | \fn bool QJsonObject::iterator::operator!=(const iterator &other) const | - | ||||||
907 | \fn bool QJsonObject::iterator::operator!=(const const_iterator &other) const | - | ||||||
908 | - | |||||||
909 | Returns \c true if \a other points to a different item than this | - | ||||||
910 | iterator; otherwise returns \c false. | - | ||||||
911 | - | |||||||
912 | \sa operator==() | - | ||||||
913 | */ | - | ||||||
914 | - | |||||||
915 | /*! \fn QJsonObject::iterator QJsonObject::iterator::operator++() | - | ||||||
916 | - | |||||||
917 | The prefix ++ operator, \c{++i}, advances the iterator to the | - | ||||||
918 | next item in the object and returns an iterator to the new current | - | ||||||
919 | item. | - | ||||||
920 | - | |||||||
921 | Calling this function on QJsonObject::end() leads to undefined results. | - | ||||||
922 | - | |||||||
923 | \sa operator--() | - | ||||||
924 | */ | - | ||||||
925 | - | |||||||
926 | /*! \fn QJsonObject::iterator QJsonObject::iterator::operator++(int) | - | ||||||
927 | - | |||||||
928 | \overload | - | ||||||
929 | - | |||||||
930 | The postfix ++ operator, \c{i++}, advances the iterator to the | - | ||||||
931 | next item in the object and returns an iterator to the previously | - | ||||||
932 | current item. | - | ||||||
933 | */ | - | ||||||
934 | - | |||||||
935 | /*! \fn QJsonObject::iterator QJsonObject::iterator::operator--() | - | ||||||
936 | - | |||||||
937 | The prefix -- operator, \c{--i}, makes the preceding item | - | ||||||
938 | current and returns an iterator pointing to the new current item. | - | ||||||
939 | - | |||||||
940 | Calling this function on QJsonObject::begin() leads to undefined | - | ||||||
941 | results. | - | ||||||
942 | - | |||||||
943 | \sa operator++() | - | ||||||
944 | */ | - | ||||||
945 | - | |||||||
946 | /*! \fn QJsonObject::iterator QJsonObject::iterator::operator--(int) | - | ||||||
947 | - | |||||||
948 | \overload | - | ||||||
949 | - | |||||||
950 | The postfix -- operator, \c{i--}, makes the preceding item | - | ||||||
951 | current and returns an iterator pointing to the previously | - | ||||||
952 | current item. | - | ||||||
953 | */ | - | ||||||
954 | - | |||||||
955 | /*! \fn QJsonObject::iterator QJsonObject::iterator::operator+(int j) const | - | ||||||
956 | - | |||||||
957 | Returns an iterator to the item at \a j positions forward from | - | ||||||
958 | this iterator. If \a j is negative, the iterator goes backward. | - | ||||||
959 | - | |||||||
960 | \sa operator-() | - | ||||||
961 | - | |||||||
962 | */ | - | ||||||
963 | - | |||||||
964 | /*! \fn QJsonObject::iterator QJsonObject::iterator::operator-(int j) const | - | ||||||
965 | - | |||||||
966 | Returns an iterator to the item at \a j positions backward from | - | ||||||
967 | this iterator. If \a j is negative, the iterator goes forward. | - | ||||||
968 | - | |||||||
969 | \sa operator+() | - | ||||||
970 | */ | - | ||||||
971 | - | |||||||
972 | /*! \fn QJsonObject::iterator &QJsonObject::iterator::operator+=(int j) | - | ||||||
973 | - | |||||||
974 | Advances the iterator by \a j items. If \a j is negative, the | - | ||||||
975 | iterator goes backward. | - | ||||||
976 | - | |||||||
977 | \sa operator-=(), operator+() | - | ||||||
978 | */ | - | ||||||
979 | - | |||||||
980 | /*! \fn QJsonObject::iterator &QJsonObject::iterator::operator-=(int j) | - | ||||||
981 | - | |||||||
982 | Makes the iterator go back by \a j items. If \a j is negative, | - | ||||||
983 | the iterator goes forward. | - | ||||||
984 | - | |||||||
985 | \sa operator+=(), operator-() | - | ||||||
986 | */ | - | ||||||
987 | - | |||||||
988 | /*! | - | ||||||
989 | \class QJsonObject::const_iterator | - | ||||||
990 | \inmodule QtCore | - | ||||||
991 | \ingroup json | - | ||||||
992 | \since 5.0 | - | ||||||
993 | \brief The QJsonObject::const_iterator class provides an STL-style const iterator for QJsonObject. | - | ||||||
994 | - | |||||||
995 | QJsonObject::const_iterator allows you to iterate over a QJsonObject. | - | ||||||
996 | If you want to modify the QJsonObject as you iterate | - | ||||||
997 | over it, you must use QJsonObject::iterator instead. It is generally | - | ||||||
998 | good practice to use QJsonObject::const_iterator on a non-const QJsonObject as | - | ||||||
999 | well, unless you need to change the QJsonObject through the iterator. | - | ||||||
1000 | Const iterators are slightly faster and improve code | - | ||||||
1001 | readability. | - | ||||||
1002 | - | |||||||
1003 | The default QJsonObject::const_iterator constructor creates an | - | ||||||
1004 | uninitialized iterator. You must initialize it using a QJsonObject | - | ||||||
1005 | function like QJsonObject::constBegin(), QJsonObject::constEnd(), or | - | ||||||
1006 | QJsonObject::find() before you can start iterating. | - | ||||||
1007 | - | |||||||
1008 | Multiple iterators can be used on the same object. Existing iterators | - | ||||||
1009 | will however become dangling if the object gets modified. | - | ||||||
1010 | - | |||||||
1011 | \sa QJsonObject::iterator, {JSON Support in Qt}, {JSON Save Game Example} | - | ||||||
1012 | */ | - | ||||||
1013 | - | |||||||
1014 | /*! \typedef QJsonObject::const_iterator::difference_type | - | ||||||
1015 | - | |||||||
1016 | \internal | - | ||||||
1017 | */ | - | ||||||
1018 | - | |||||||
1019 | /*! \typedef QJsonObject::const_iterator::iterator_category | - | ||||||
1020 | - | |||||||
1021 | A synonym for \e {std::random_access_iterator_tag} indicating | - | ||||||
1022 | this iterator is a random-access iterator. | - | ||||||
1023 | - | |||||||
1024 | \note In Qt versions before 5.6, this was set by mistake to | - | ||||||
1025 | \e {std::bidirectional_iterator_tag}. | - | ||||||
1026 | */ | - | ||||||
1027 | - | |||||||
1028 | /*! \typedef QJsonObject::const_iterator::reference | - | ||||||
1029 | - | |||||||
1030 | \internal | - | ||||||
1031 | */ | - | ||||||
1032 | - | |||||||
1033 | /*! \typedef QJsonObject::const_iterator::value_type | - | ||||||
1034 | - | |||||||
1035 | \internal | - | ||||||
1036 | */ | - | ||||||
1037 | - | |||||||
1038 | /*! \typedef QJsonObject::const_iterator::pointer | - | ||||||
1039 | - | |||||||
1040 | \internal | - | ||||||
1041 | */ | - | ||||||
1042 | - | |||||||
1043 | /*! \fn QJsonObject::const_iterator::const_iterator() | - | ||||||
1044 | - | |||||||
1045 | Constructs an uninitialized iterator. | - | ||||||
1046 | - | |||||||
1047 | Functions like key(), value(), and operator++() must not be | - | ||||||
1048 | called on an uninitialized iterator. Use operator=() to assign a | - | ||||||
1049 | value to it before using it. | - | ||||||
1050 | - | |||||||
1051 | \sa QJsonObject::constBegin(), QJsonObject::constEnd() | - | ||||||
1052 | */ | - | ||||||
1053 | - | |||||||
1054 | /*! \fn QJsonObject::const_iterator::const_iterator(const QJsonObject *obj, int index) | - | ||||||
1055 | \internal | - | ||||||
1056 | */ | - | ||||||
1057 | - | |||||||
1058 | /*! \fn QJsonObject::const_iterator::const_iterator(const iterator &other) | - | ||||||
1059 | - | |||||||
1060 | Constructs a copy of \a other. | - | ||||||
1061 | */ | - | ||||||
1062 | - | |||||||
1063 | /*! \fn QString QJsonObject::const_iterator::key() const | - | ||||||
1064 | - | |||||||
1065 | Returns the current item's key. | - | ||||||
1066 | - | |||||||
1067 | \sa value() | - | ||||||
1068 | */ | - | ||||||
1069 | - | |||||||
1070 | /*! \fn QJsonValue QJsonObject::const_iterator::value() const | - | ||||||
1071 | - | |||||||
1072 | Returns the current item's value. | - | ||||||
1073 | - | |||||||
1074 | \sa key(), operator*() | - | ||||||
1075 | */ | - | ||||||
1076 | - | |||||||
1077 | /*! \fn QJsonValue QJsonObject::const_iterator::operator*() const | - | ||||||
1078 | - | |||||||
1079 | Returns the current item's value. | - | ||||||
1080 | - | |||||||
1081 | Same as value(). | - | ||||||
1082 | - | |||||||
1083 | \sa key() | - | ||||||
1084 | */ | - | ||||||
1085 | - | |||||||
1086 | /*! \fn QJsonValue *QJsonObject::const_iterator::operator->() const | - | ||||||
1087 | - | |||||||
1088 | Returns a pointer to the current item. | - | ||||||
1089 | */ | - | ||||||
1090 | - | |||||||
1091 | /*! \fn bool QJsonObject::const_iterator::operator==(const const_iterator &other) const | - | ||||||
1092 | \fn bool QJsonObject::const_iterator::operator==(const iterator &other) const | - | ||||||
1093 | - | |||||||
1094 | Returns \c true if \a other points to the same item as this | - | ||||||
1095 | iterator; otherwise returns \c false. | - | ||||||
1096 | - | |||||||
1097 | \sa operator!=() | - | ||||||
1098 | */ | - | ||||||
1099 | - | |||||||
1100 | /*! \fn bool QJsonObject::const_iterator::operator!=(const const_iterator &other) const | - | ||||||
1101 | \fn bool QJsonObject::const_iterator::operator!=(const iterator &other) const | - | ||||||
1102 | - | |||||||
1103 | Returns \c true if \a other points to a different item than this | - | ||||||
1104 | iterator; otherwise returns \c false. | - | ||||||
1105 | - | |||||||
1106 | \sa operator==() | - | ||||||
1107 | */ | - | ||||||
1108 | - | |||||||
1109 | /*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator++() | - | ||||||
1110 | - | |||||||
1111 | The prefix ++ operator, \c{++i}, advances the iterator to the | - | ||||||
1112 | next item in the object and returns an iterator to the new current | - | ||||||
1113 | item. | - | ||||||
1114 | - | |||||||
1115 | Calling this function on QJsonObject::end() leads to undefined results. | - | ||||||
1116 | - | |||||||
1117 | \sa operator--() | - | ||||||
1118 | */ | - | ||||||
1119 | - | |||||||
1120 | /*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator++(int) | - | ||||||
1121 | - | |||||||
1122 | \overload | - | ||||||
1123 | - | |||||||
1124 | The postfix ++ operator, \c{i++}, advances the iterator to the | - | ||||||
1125 | next item in the object and returns an iterator to the previously | - | ||||||
1126 | current item. | - | ||||||
1127 | */ | - | ||||||
1128 | - | |||||||
1129 | /*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator--() | - | ||||||
1130 | - | |||||||
1131 | The prefix -- operator, \c{--i}, makes the preceding item | - | ||||||
1132 | current and returns an iterator pointing to the new current item. | - | ||||||
1133 | - | |||||||
1134 | Calling this function on QJsonObject::begin() leads to undefined | - | ||||||
1135 | results. | - | ||||||
1136 | - | |||||||
1137 | \sa operator++() | - | ||||||
1138 | */ | - | ||||||
1139 | - | |||||||
1140 | /*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator--(int) | - | ||||||
1141 | - | |||||||
1142 | \overload | - | ||||||
1143 | - | |||||||
1144 | The postfix -- operator, \c{i--}, makes the preceding item | - | ||||||
1145 | current and returns an iterator pointing to the previously | - | ||||||
1146 | current item. | - | ||||||
1147 | */ | - | ||||||
1148 | - | |||||||
1149 | /*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator+(int j) const | - | ||||||
1150 | - | |||||||
1151 | Returns an iterator to the item at \a j positions forward from | - | ||||||
1152 | this iterator. If \a j is negative, the iterator goes backward. | - | ||||||
1153 | - | |||||||
1154 | This operation can be slow for large \a j values. | - | ||||||
1155 | - | |||||||
1156 | \sa operator-() | - | ||||||
1157 | */ | - | ||||||
1158 | - | |||||||
1159 | /*! \fn QJsonObject::const_iterator QJsonObject::const_iterator::operator-(int j) const | - | ||||||
1160 | - | |||||||
1161 | Returns an iterator to the item at \a j positions backward from | - | ||||||
1162 | this iterator. If \a j is negative, the iterator goes forward. | - | ||||||
1163 | - | |||||||
1164 | This operation can be slow for large \a j values. | - | ||||||
1165 | - | |||||||
1166 | \sa operator+() | - | ||||||
1167 | */ | - | ||||||
1168 | - | |||||||
1169 | /*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator+=(int j) | - | ||||||
1170 | - | |||||||
1171 | Advances the iterator by \a j items. If \a j is negative, the | - | ||||||
1172 | iterator goes backward. | - | ||||||
1173 | - | |||||||
1174 | This operation can be slow for large \a j values. | - | ||||||
1175 | - | |||||||
1176 | \sa operator-=(), operator+() | - | ||||||
1177 | */ | - | ||||||
1178 | - | |||||||
1179 | /*! \fn QJsonObject::const_iterator &QJsonObject::const_iterator::operator-=(int j) | - | ||||||
1180 | - | |||||||
1181 | Makes the iterator go back by \a j items. If \a j is negative, | - | ||||||
1182 | the iterator goes forward. | - | ||||||
1183 | - | |||||||
1184 | This operation can be slow for large \a j values. | - | ||||||
1185 | - | |||||||
1186 | \sa operator+=(), operator-() | - | ||||||
1187 | */ | - | ||||||
1188 | - | |||||||
1189 | - | |||||||
1190 | /*! | - | ||||||
1191 | \internal | - | ||||||
1192 | */ | - | ||||||
1193 | void QJsonObject::detach(uint reserve) | - | ||||||
1194 | { | - | ||||||
1195 | Q_UNUSED(reserve) | - | ||||||
1196 | Q_ASSERT(!reserve); | - | ||||||
1197 | detach2(reserve); | - | ||||||
1198 | } | - | ||||||
1199 | - | |||||||
1200 | bool QJsonObject::detach2(uint reserve) | - | ||||||
1201 | { | - | ||||||
1202 | if (!d) { | - | ||||||
1203 | if (reserve >= QJsonPrivate::Value::MaxSize) { | - | ||||||
1204 | qWarning("QJson: Document too large to store in data structure"); | - | ||||||
1205 | return false; | - | ||||||
1206 | } | - | ||||||
1207 | d = new QJsonPrivate::Data(reserve, QJsonValue::Object); | - | ||||||
1208 | o = static_cast<QJsonPrivate::Object *>(d->header->root()); | - | ||||||
1209 | d->ref.ref(); | - | ||||||
1210 | return true; | - | ||||||
1211 | } | - | ||||||
1212 | if (reserve == 0 && d->ref.load() == 1) | - | ||||||
1213 | return true; | - | ||||||
1214 | - | |||||||
1215 | QJsonPrivate::Data *x = d->clone(o, reserve); | - | ||||||
1216 | if (!x) | - | ||||||
1217 | return false; | - | ||||||
1218 | x->ref.ref(); | - | ||||||
1219 | if (!d->ref.deref()) | - | ||||||
1220 | delete d; | - | ||||||
1221 | d = x; | - | ||||||
1222 | o = static_cast<QJsonPrivate::Object *>(d->header->root()); | - | ||||||
1223 | return true; | - | ||||||
1224 | } | - | ||||||
1225 | - | |||||||
1226 | /*! | - | ||||||
1227 | \internal | - | ||||||
1228 | */ | - | ||||||
1229 | void QJsonObject::compact() | - | ||||||
1230 | { | - | ||||||
1231 | if (!d || !d->compactionCounter) | - | ||||||
1232 | return; | - | ||||||
1233 | - | |||||||
1234 | detach2(); | - | ||||||
1235 | d->compact(); | - | ||||||
1236 | o = static_cast<QJsonPrivate::Object *>(d->header->root()); | - | ||||||
1237 | } | - | ||||||
1238 | - | |||||||
1239 | /*! | - | ||||||
1240 | \internal | - | ||||||
1241 | */ | - | ||||||
1242 | QString QJsonObject::keyAt(int i) const | - | ||||||
1243 | { | - | ||||||
1244 | Q_ASSERT(o && i >= 0 && i < (int)o->length); | - | ||||||
1245 | - | |||||||
1246 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
1247 | return e->key(); | - | ||||||
1248 | } | - | ||||||
1249 | - | |||||||
1250 | /*! | - | ||||||
1251 | \internal | - | ||||||
1252 | */ | - | ||||||
1253 | QJsonValue QJsonObject::valueAt(int i) const | - | ||||||
1254 | { | - | ||||||
1255 | if (!o || i < 0 || i >= (int)o->length) | - | ||||||
1256 | return QJsonValue(QJsonValue::Undefined); | - | ||||||
1257 | - | |||||||
1258 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
1259 | return QJsonValue(d, o, e->value); | - | ||||||
1260 | } | - | ||||||
1261 | - | |||||||
1262 | /*! | - | ||||||
1263 | \internal | - | ||||||
1264 | */ | - | ||||||
1265 | void QJsonObject::setValueAt(int i, const QJsonValue &val) | - | ||||||
1266 | { | - | ||||||
1267 | Q_ASSERT(o && i >= 0 && i < (int)o->length); | - | ||||||
1268 | - | |||||||
1269 | QJsonPrivate::Entry *e = o->entryAt(i); | - | ||||||
1270 | insert(e->key(), val); | - | ||||||
1271 | } | - | ||||||
1272 | - | |||||||
1273 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) | - | ||||||
1274 | QDebug operator<<(QDebug dbg, const QJsonObject &o) | - | ||||||
1275 | { | - | ||||||
1276 | QDebugStateSaver saver(dbg); | - | ||||||
1277 | if (!o.o) { | - | ||||||
1278 | dbg << "QJsonObject()"; | - | ||||||
1279 | return dbg; | - | ||||||
1280 | } | - | ||||||
1281 | QByteArray json; | - | ||||||
1282 | QJsonPrivate::Writer::objectToJson(o.o, json, 0, true); | - | ||||||
1283 | dbg.nospace() << "QJsonObject(" | - | ||||||
1284 | << json.constData() // print as utf-8 string without extra quotation marks | - | ||||||
1285 | << ")"; | - | ||||||
1286 | return dbg; | - | ||||||
1287 | } | - | ||||||
1288 | #endif | - | ||||||
1289 | - | |||||||
1290 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |