1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include <qjsonobject.h> |
5 | #include <qjsonvalue.h> |
6 | #include <qjsonarray.h> |
7 | #include <qjsondocument.h> |
8 | #include <qstringlist.h> |
9 | #include <qcborarray.h> |
10 | #include <qvariant.h> |
11 | #include <qdebug.h> |
12 | |
13 | #include <private/qcborvalue_p.h> |
14 | #include <private/qjson_p.h> |
15 | |
16 | #include "qjsonwriter_p.h" |
17 | |
18 | QT_BEGIN_NAMESPACE |
19 | |
20 | /*! |
21 | \class QJsonArray |
22 | \inmodule QtCore |
23 | \ingroup json |
24 | \ingroup shared |
25 | \ingroup qtserialization |
26 | \reentrant |
27 | \since 5.0 |
28 | |
29 | \brief The QJsonArray class encapsulates a JSON array. |
30 | |
31 | \compares equality |
32 | \compareswith equality QJsonValue |
33 | \endcompareswith |
34 | |
35 | A JSON array is a list of values. The list can be manipulated by inserting and |
36 | removing QJsonValue's from the array. |
37 | |
38 | A QJsonArray can be converted to and from a QVariantList. You can query the |
39 | number of entries with size(), insert(), and removeAt() entries from it |
40 | and iterate over its content using the standard C++ iterator pattern. |
41 | |
42 | QJsonArray is an implicitly shared class and shares the data with the document |
43 | it has been created from as long as it is not being modified. |
44 | |
45 | You can convert the array to and from text based JSON through QJsonDocument. |
46 | |
47 | \sa {JSON Support in Qt}, {Saving and Loading a Game} |
48 | */ |
49 | |
50 | /*! |
51 | \typedef QJsonArray::Iterator |
52 | |
53 | Qt-style synonym for QJsonArray::iterator. |
54 | */ |
55 | |
56 | /*! |
57 | \typedef QJsonArray::ConstIterator |
58 | |
59 | Qt-style synonym for QJsonArray::const_iterator. |
60 | */ |
61 | |
62 | /*! |
63 | \typedef QJsonArray::size_type |
64 | |
65 | Typedef for qsizetype. Provided for STL compatibility. |
66 | */ |
67 | |
68 | /*! |
69 | \typedef QJsonArray::value_type |
70 | |
71 | Typedef for QJsonValue. Provided for STL compatibility. |
72 | */ |
73 | |
74 | /*! |
75 | \typedef QJsonArray::difference_type |
76 | |
77 | Typedef for qsizetype. Provided for STL compatibility. |
78 | */ |
79 | |
80 | /*! |
81 | \typedef QJsonArray::pointer |
82 | |
83 | Typedef for QJsonValue *. Provided for STL compatibility. |
84 | */ |
85 | |
86 | /*! |
87 | \typedef QJsonArray::const_pointer |
88 | |
89 | Typedef for const QJsonValue *. Provided for STL compatibility. |
90 | */ |
91 | |
92 | /*! |
93 | \typedef QJsonArray::reference |
94 | |
95 | Typedef for QJsonValue &. Provided for STL compatibility. |
96 | */ |
97 | |
98 | /*! |
99 | \typedef QJsonArray::const_reference |
100 | |
101 | Typedef for const QJsonValue &. Provided for STL compatibility. |
102 | */ |
103 | |
104 | /*! |
105 | Creates an empty array. |
106 | */ |
107 | QJsonArray::QJsonArray() = default; |
108 | |
109 | /*! |
110 | \fn QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args) |
111 | \since 5.4 |
112 | Creates an array initialized from \a args initialization list. |
113 | |
114 | QJsonArray can be constructed in a way similar to JSON notation, |
115 | for example: |
116 | \code |
117 | QJsonArray array = { 1, 2.2, QString() }; |
118 | \endcode |
119 | */ |
120 | |
121 | /*! |
122 | \internal |
123 | */ |
124 | QJsonArray::QJsonArray(QCborContainerPrivate *array) |
125 | : a(array) |
126 | { |
127 | } |
128 | |
129 | /*! |
130 | Deletes the array. |
131 | */ |
132 | QJsonArray::~QJsonArray() = default; |
133 | |
134 | QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args) |
135 | { |
136 | for (const auto & arg : args) |
137 | append(value: arg); |
138 | } |
139 | |
140 | /*! |
141 | Creates a copy of \a other. |
142 | |
143 | Since QJsonArray is implicitly shared, the copy is shallow |
144 | as long as the object doesn't get modified. |
145 | */ |
146 | QJsonArray::QJsonArray(const QJsonArray &other) noexcept = default; |
147 | |
148 | /*! |
149 | \since 5.10 |
150 | |
151 | Move-constructs a QJsonArray from \a other. |
152 | */ |
153 | QJsonArray::QJsonArray(QJsonArray &&other) noexcept |
154 | : a(other.a) |
155 | { |
156 | other.a = nullptr; |
157 | } |
158 | |
159 | /*! |
160 | Assigns \a other to this array. |
161 | */ |
162 | QJsonArray &QJsonArray::operator =(const QJsonArray &other) noexcept = default; |
163 | |
164 | /*! |
165 | \fn QJsonArray &QJsonArray::operator =(QJsonArray &&other) |
166 | \since 5.10 |
167 | |
168 | Move-assigns \a other to this array. |
169 | */ |
170 | |
171 | /*! |
172 | \fn void QJsonArray::swap(QJsonArray &other) |
173 | \since 5.10 |
174 | \memberswap{array} |
175 | */ |
176 | |
177 | /*! \fn QJsonArray &QJsonArray::operator+=(const QJsonValue &value) |
178 | |
179 | Appends \a value to the array, and returns a reference to the array itself. |
180 | |
181 | \since 5.3 |
182 | \sa append(), operator<<() |
183 | */ |
184 | |
185 | /*! \fn QJsonArray QJsonArray::operator+(const QJsonValue &value) const |
186 | |
187 | Returns an array that contains all the items in this array followed |
188 | by the provided \a value. |
189 | |
190 | \since 5.3 |
191 | \sa operator+=() |
192 | */ |
193 | |
194 | /*! \fn QJsonArray &QJsonArray::operator<<(const QJsonValue &value) |
195 | |
196 | Appends \a value to the array, and returns a reference to the array itself. |
197 | |
198 | \since 5.3 |
199 | \sa operator+=(), append() |
200 | */ |
201 | |
202 | /*! |
203 | Converts the string list \a list to a QJsonArray. |
204 | |
205 | The values in \a list will be converted to JSON values. |
206 | |
207 | \sa toVariantList(), QJsonValue::fromVariant() |
208 | */ |
209 | QJsonArray QJsonArray::fromStringList(const QStringList &list) |
210 | { |
211 | QJsonArray array; |
212 | for (QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it) |
213 | array.append(value: QJsonValue(*it)); |
214 | return array; |
215 | } |
216 | |
217 | #ifndef QT_NO_VARIANT |
218 | /*! |
219 | Converts the variant list \a list to a QJsonArray. |
220 | |
221 | The QVariant values in \a list will be converted to JSON values. |
222 | |
223 | \note Conversion from \l QVariant is not completely lossless. Please see |
224 | the documentation in QJsonValue::fromVariant() for more information. |
225 | |
226 | \sa toVariantList(), QJsonValue::fromVariant() |
227 | */ |
228 | QJsonArray QJsonArray::fromVariantList(const QVariantList &list) |
229 | { |
230 | return QJsonPrivate::Variant::toJsonArray(list); |
231 | } |
232 | |
233 | /*! |
234 | Converts this object to a QVariantList. |
235 | |
236 | Returns the created map. |
237 | */ |
238 | QVariantList QJsonArray::toVariantList() const |
239 | { |
240 | return QCborArray::fromJsonArray(array: *this).toVariantList(); |
241 | } |
242 | #endif // !QT_NO_VARIANT |
243 | |
244 | |
245 | /*! |
246 | Returns the number of values stored in the array. |
247 | */ |
248 | qsizetype QJsonArray::size() const |
249 | { |
250 | return a ? a->elements.size() : 0; |
251 | } |
252 | |
253 | /*! |
254 | \fn QJsonArray::count() const |
255 | |
256 | Same as size(). |
257 | |
258 | \sa size() |
259 | */ |
260 | |
261 | /*! |
262 | Returns \c true if the object is empty. This is the same as size() == 0. |
263 | |
264 | \sa size() |
265 | */ |
266 | bool QJsonArray::isEmpty() const |
267 | { |
268 | return a == nullptr || a->elements.isEmpty(); |
269 | } |
270 | |
271 | /*! |
272 | Returns a QJsonValue representing the value for index \a i. |
273 | |
274 | The returned QJsonValue is \c Undefined, if \a i is out of bounds. |
275 | |
276 | */ |
277 | QJsonValue QJsonArray::at(qsizetype i) const |
278 | { |
279 | if (!a || i < 0 || i >= a->elements.size()) |
280 | return QJsonValue(QJsonValue::Undefined); |
281 | |
282 | return QJsonPrivate::Value::fromTrustedCbor(v: a->valueAt(idx: i)); |
283 | } |
284 | |
285 | /*! |
286 | Returns the first value stored in the array. |
287 | |
288 | Same as \c at(0). |
289 | |
290 | \sa at() |
291 | */ |
292 | QJsonValue QJsonArray::first() const |
293 | { |
294 | return at(i: 0); |
295 | } |
296 | |
297 | /*! |
298 | Returns the last value stored in the array. |
299 | |
300 | Same as \c{at(size() - 1)}. |
301 | |
302 | \sa at() |
303 | */ |
304 | QJsonValue QJsonArray::last() const |
305 | { |
306 | return at(i: a ? (a->elements.size() - 1) : 0); |
307 | } |
308 | |
309 | /*! |
310 | Inserts \a value at the beginning of the array. |
311 | |
312 | This is the same as \c{insert(0, value)} and will prepend \a value to the array. |
313 | |
314 | \sa append(), insert() |
315 | */ |
316 | void QJsonArray::prepend(const QJsonValue &value) |
317 | { |
318 | insert(i: 0, value); |
319 | } |
320 | |
321 | /*! |
322 | Inserts \a value at the end of the array. |
323 | |
324 | \sa prepend(), insert() |
325 | */ |
326 | void QJsonArray::append(const QJsonValue &value) |
327 | { |
328 | insert(i: a ? a->elements.size() : 0, value); |
329 | } |
330 | |
331 | /*! |
332 | Removes the value at index position \a i. \a i must be a valid |
333 | index position in the array (i.e., \c{0 <= i < size()}). |
334 | |
335 | \sa insert(), replace() |
336 | */ |
337 | void QJsonArray::removeAt(qsizetype i) |
338 | { |
339 | if (!a || i < 0 || i >= a->elements.size()) |
340 | return; |
341 | detach(); |
342 | a->removeAt(idx: i); |
343 | } |
344 | |
345 | /*! \fn void QJsonArray::removeFirst() |
346 | |
347 | Removes the first item in the array. Calling this function is |
348 | equivalent to calling \c{removeAt(0)}. The array must not be empty. If |
349 | the array can be empty, call isEmpty() before calling this |
350 | function. |
351 | |
352 | \sa removeAt(), removeLast() |
353 | */ |
354 | |
355 | /*! \fn void QJsonArray::removeLast() |
356 | |
357 | Removes the last item in the array. Calling this function is |
358 | equivalent to calling \c{removeAt(size() - 1)}. The array must not be |
359 | empty. If the array can be empty, call isEmpty() before calling |
360 | this function. |
361 | |
362 | \sa removeAt(), removeFirst() |
363 | */ |
364 | |
365 | /*! |
366 | Removes the item at index position \a i and returns it. \a i must |
367 | be a valid index position in the array (i.e., \c{0 <= i < size()}). |
368 | |
369 | If you don't use the return value, removeAt() is more efficient. |
370 | |
371 | \sa removeAt() |
372 | */ |
373 | QJsonValue QJsonArray::takeAt(qsizetype i) |
374 | { |
375 | if (!a || i < 0 || i >= a->elements.size()) |
376 | return QJsonValue(QJsonValue::Undefined); |
377 | |
378 | detach(); |
379 | const QJsonValue v = QJsonPrivate::Value::fromTrustedCbor(v: a->extractAt(idx: i)); |
380 | a->removeAt(idx: i); |
381 | return v; |
382 | } |
383 | |
384 | /*! |
385 | Inserts \a value at index position \a i in the array. If \a i |
386 | is \c 0, the value is prepended to the array. If \a i is size(), the |
387 | value is appended to the array. |
388 | |
389 | \sa append(), prepend(), replace(), removeAt() |
390 | */ |
391 | void QJsonArray::insert(qsizetype i, const QJsonValue &value) |
392 | { |
393 | if (a) |
394 | detach(reserve: a->elements.size() + 1); |
395 | else |
396 | a = new QCborContainerPrivate; |
397 | |
398 | Q_ASSERT (i >= 0 && i <= a->elements.size()); |
399 | a->insertAt(idx: i, value: value.type() == QJsonValue::Undefined ? QCborValue(nullptr) |
400 | : QCborValue::fromJsonValue(v: value)); |
401 | } |
402 | |
403 | /*! |
404 | \fn QJsonArray::iterator QJsonArray::insert(iterator before, const QJsonValue &value) |
405 | |
406 | Inserts \a value before the position pointed to by \a before, and returns an iterator |
407 | pointing to the newly inserted item. |
408 | |
409 | \sa erase(), insert() |
410 | */ |
411 | |
412 | /*! |
413 | \fn QJsonArray::iterator QJsonArray::erase(iterator it) |
414 | |
415 | Removes the item pointed to by \a it, and returns an iterator pointing to the |
416 | next item. |
417 | |
418 | \sa removeAt() |
419 | */ |
420 | |
421 | /*! |
422 | Replaces the item at index position \a i with \a value. \a i must |
423 | be a valid index position in the array (i.e., \c{0 <= i < size()}). |
424 | |
425 | \sa operator[](), removeAt() |
426 | */ |
427 | void QJsonArray::replace(qsizetype i, const QJsonValue &value) |
428 | { |
429 | Q_ASSERT (a && i >= 0 && i < a->elements.size()); |
430 | detach(); |
431 | a->replaceAt(idx: i, value: QCborValue::fromJsonValue(v: value)); |
432 | } |
433 | |
434 | /*! |
435 | Returns \c true if the array contains an occurrence of \a value, otherwise \c false. |
436 | |
437 | \sa count() |
438 | */ |
439 | bool QJsonArray::contains(const QJsonValue &value) const |
440 | { |
441 | for (qsizetype i = 0; i < size(); i++) { |
442 | if (at(i) == value) |
443 | return true; |
444 | } |
445 | return false; |
446 | } |
447 | |
448 | /*! |
449 | Returns the value at index position \a i as a modifiable reference. |
450 | \a i must be a valid index position in the array (i.e., \c{0 <= i < |
451 | size()}). |
452 | |
453 | The return value is of type QJsonValueRef, a helper class for QJsonArray |
454 | and QJsonObject. When you get an object of type QJsonValueRef, you can |
455 | use it as if it were a reference to a QJsonValue. If you assign to it, |
456 | the assignment will apply to the character in the QJsonArray of QJsonObject |
457 | from which you got the reference. |
458 | |
459 | \sa at() |
460 | */ |
461 | QJsonValueRef QJsonArray::operator [](qsizetype i) |
462 | { |
463 | Q_ASSERT(a && i >= 0 && i < a->elements.size()); |
464 | return QJsonValueRef(this, i); |
465 | } |
466 | |
467 | /*! |
468 | \overload |
469 | |
470 | Same as at(). |
471 | */ |
472 | QJsonValue QJsonArray::operator[](qsizetype i) const |
473 | { |
474 | return at(i); |
475 | } |
476 | |
477 | bool comparesEqual(const QJsonArray &lhs, const QJsonArray &rhs) |
478 | { |
479 | if (lhs.a == rhs.a) |
480 | return true; |
481 | |
482 | if (!lhs.a) |
483 | return !rhs.a->elements.size(); |
484 | if (!rhs.a) |
485 | return !lhs.a->elements.size(); |
486 | if (lhs.a->elements.size() != rhs.a->elements.size()) |
487 | return false; |
488 | |
489 | for (qsizetype i = 0; i < lhs.a->elements.size(); ++i) { |
490 | if (lhs.a->valueAt(idx: i) != rhs.a->valueAt(idx: i)) |
491 | return false; |
492 | } |
493 | return true; |
494 | } |
495 | |
496 | bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) |
497 | { |
498 | return lhs == rhs.toArray(); |
499 | } |
500 | |
501 | /*! \fn bool QJsonArray::operator==(const QJsonArray &lhs, const QJsonArray &rhs) |
502 | |
503 | Returns \c true if \a lhs array is equal to \a rhs, \c false otherwise. |
504 | */ |
505 | |
506 | /*! \fn bool QJsonArray::operator!=(const QJsonArray &lhs, const QJsonArray &rhs) |
507 | |
508 | Returns \c true if \a lhs array is not equal to \a rhs, \c false otherwise. |
509 | */ |
510 | |
511 | /*! \fn QJsonArray::iterator QJsonArray::begin() |
512 | |
513 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in |
514 | the array. |
515 | |
516 | \sa constBegin(), end() |
517 | */ |
518 | |
519 | /*! \fn QJsonArray::const_iterator QJsonArray::begin() const |
520 | |
521 | \overload |
522 | */ |
523 | |
524 | /*! \fn QJsonArray::const_iterator QJsonArray::constBegin() const |
525 | |
526 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item |
527 | in the array. |
528 | |
529 | \sa begin(), constEnd() |
530 | */ |
531 | |
532 | /*! \fn QJsonArray::const_iterator QJsonArray::cbegin() const |
533 | |
534 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item |
535 | in the array. |
536 | |
537 | \sa begin(), cend() |
538 | */ |
539 | |
540 | /*! \fn QJsonArray::iterator QJsonArray::end() |
541 | |
542 | Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item |
543 | after the last item in the array. |
544 | |
545 | \sa begin(), constEnd() |
546 | */ |
547 | |
548 | /*! \fn const_iterator QJsonArray::end() const |
549 | |
550 | \overload |
551 | */ |
552 | |
553 | /*! \fn QJsonArray::const_iterator QJsonArray::constEnd() const |
554 | |
555 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary |
556 | item after the last item in the array. |
557 | |
558 | \sa constBegin(), end() |
559 | */ |
560 | |
561 | /*! \fn QJsonArray::const_iterator QJsonArray::cend() const |
562 | |
563 | Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary |
564 | item after the last item in the array. |
565 | |
566 | \sa cbegin(), end() |
567 | */ |
568 | |
569 | /*! \fn void QJsonArray::push_back(const QJsonValue &value) |
570 | |
571 | This function is provided for STL compatibility. It is equivalent |
572 | to \l{QJsonArray::append()}{append(value)} and will append \a value to the array. |
573 | */ |
574 | |
575 | /*! \fn void QJsonArray::push_front(const QJsonValue &value) |
576 | |
577 | This function is provided for STL compatibility. It is equivalent |
578 | to \l{QJsonArray::prepend()}{prepend(value)} and will prepend \a value to the array. |
579 | */ |
580 | |
581 | /*! \fn void QJsonArray::pop_front() |
582 | |
583 | This function is provided for STL compatibility. It is equivalent |
584 | to removeFirst(). The array must not be empty. If the array can be |
585 | empty, call isEmpty() before calling this function. |
586 | */ |
587 | |
588 | /*! \fn void QJsonArray::pop_back() |
589 | |
590 | This function is provided for STL compatibility. It is equivalent |
591 | to removeLast(). The array must not be empty. If the array can be |
592 | empty, call isEmpty() before calling this function. |
593 | */ |
594 | |
595 | /*! \fn bool QJsonArray::empty() const |
596 | |
597 | This function is provided for STL compatibility. It is equivalent |
598 | to isEmpty() and returns \c true if the array is empty. |
599 | */ |
600 | |
601 | /*! \class QJsonArray::iterator |
602 | \inmodule QtCore |
603 | \brief The QJsonArray::iterator class provides an STL-style non-const iterator for QJsonArray. |
604 | |
605 | \compares strong |
606 | \compareswith strong QJsonArray::const_iterator |
607 | \endcompareswith |
608 | |
609 | QJsonArray::iterator allows you to iterate over a QJsonArray |
610 | and to modify the array item associated with the |
611 | iterator. If you want to iterate over a const QJsonArray, use |
612 | QJsonArray::const_iterator instead. It is generally a good practice to |
613 | use QJsonArray::const_iterator on a non-const QJsonArray as well, unless |
614 | you need to change the QJsonArray through the iterator. Const |
615 | iterators are slightly faster and improves code readability. |
616 | |
617 | The default QJsonArray::iterator constructor creates an uninitialized |
618 | iterator. You must initialize it using a QJsonArray function like |
619 | QJsonArray::begin(), QJsonArray::end(), or QJsonArray::insert() before you can |
620 | start iterating. |
621 | |
622 | Most QJsonArray functions accept an integer index rather than an |
623 | iterator. For that reason, iterators are rarely useful in |
624 | connection with QJsonArray. One place where STL-style iterators do |
625 | make sense is as arguments to \l{generic algorithms}. |
626 | |
627 | Multiple iterators can be used on the same array. However, be |
628 | aware that any non-const function call performed on the QJsonArray |
629 | will render all existing iterators undefined. |
630 | |
631 | \sa QJsonArray::const_iterator |
632 | */ |
633 | |
634 | /*! \typedef QJsonArray::iterator::iterator_category |
635 | |
636 | A synonym for \e {std::random_access_iterator_tag} indicating |
637 | this iterator is a random access iterator. |
638 | */ |
639 | |
640 | /*! \typedef QJsonArray::iterator::difference_type |
641 | |
642 | \internal |
643 | */ |
644 | |
645 | /*! \typedef QJsonArray::iterator::value_type |
646 | |
647 | \internal |
648 | */ |
649 | |
650 | /*! \typedef QJsonArray::iterator::reference |
651 | |
652 | \internal |
653 | */ |
654 | |
655 | /*! \typedef QJsonArray::iterator::pointer |
656 | |
657 | \internal |
658 | */ |
659 | |
660 | /*! \fn QJsonArray::iterator::iterator() |
661 | |
662 | Constructs an uninitialized iterator. |
663 | |
664 | Functions like operator*() and operator++() should not be called |
665 | on an uninitialized iterator. Use operator=() to assign a value |
666 | to it before using it. |
667 | |
668 | \sa QJsonArray::begin(), QJsonArray::end() |
669 | */ |
670 | |
671 | /*! \fn QJsonArray::iterator::iterator(QJsonArray *array, qsizetype index) |
672 | \internal |
673 | */ |
674 | |
675 | /*! \fn QJsonValueRef QJsonArray::iterator::operator*() const |
676 | |
677 | |
678 | Returns a modifiable reference to the current item. |
679 | |
680 | You can change the value of an item by using operator*() on the |
681 | left side of an assignment. |
682 | |
683 | The return value is of type QJsonValueRef, a helper class for QJsonArray |
684 | and QJsonObject. When you get an object of type QJsonValueRef, you can |
685 | use it as if it were a reference to a QJsonValue. If you assign to it, |
686 | the assignment will apply to the character in the QJsonArray of QJsonObject |
687 | from which you got the reference. |
688 | */ |
689 | |
690 | /*! \fn QJsonValueRef *QJsonArray::iterator::operator->() const |
691 | |
692 | Returns a pointer to a modifiable reference to the current item. |
693 | */ |
694 | |
695 | /*! \fn QJsonValueRef QJsonArray::iterator::operator[](qsizetype j) const |
696 | |
697 | Returns a modifiable reference to the item at offset \a j from the |
698 | item pointed to by this iterator (the item at position \c{*this + j}). |
699 | |
700 | This function is provided to make QJsonArray iterators behave like C++ |
701 | pointers. |
702 | |
703 | The return value is of type QJsonValueRef, a helper class for QJsonArray |
704 | and QJsonObject. When you get an object of type QJsonValueRef, you can |
705 | use it as if it were a reference to a QJsonValue. If you assign to it, |
706 | the assignment will apply to the element in the QJsonArray or QJsonObject |
707 | from which you got the reference. |
708 | |
709 | \sa operator+() |
710 | */ |
711 | |
712 | /*! |
713 | \fn bool QJsonArray::iterator::operator==(const iterator &lhs, const iterator &rhs) |
714 | \fn bool QJsonArray::iterator::operator==(const iterator &lhs, const const_iterator &rhs) |
715 | |
716 | Returns \c true if \a lhs points to the same item as \a rhs |
717 | iterator; otherwise returns \c false. |
718 | |
719 | \sa operator!=() |
720 | */ |
721 | |
722 | /*! |
723 | \fn bool QJsonArray::iterator::operator!=(const iterator &lhs, const iterator &rhs) |
724 | \fn bool QJsonArray::iterator::operator!=(const iterator &lhs, const const_iterator &rhs) |
725 | |
726 | Returns \c true if \a lhs points to a different item than \a rhs |
727 | iterator; otherwise returns \c false. |
728 | |
729 | \sa operator==() |
730 | */ |
731 | |
732 | /*! |
733 | \fn bool QJsonArray::iterator::operator<(const iterator &lhs, const iterator &rhs) |
734 | \fn bool QJsonArray::iterator::operator<(const iterator &lhs, const const_iterator &rhs) |
735 | |
736 | Returns \c true if the item pointed to by \a lhs iterator is less than |
737 | the item pointed to by the \a rhs iterator. |
738 | */ |
739 | |
740 | /*! |
741 | \fn bool QJsonArray::iterator::operator<=(const iterator &lhs, const iterator &rhs) |
742 | \fn bool QJsonArray::iterator::operator<=(const iterator &lhs, const const_iterator &rhs) |
743 | |
744 | Returns \c true if the item pointed to by \a lhs iterator is less than |
745 | or equal to the item pointed to by the \a rhs iterator. |
746 | */ |
747 | |
748 | /*! |
749 | \fn bool QJsonArray::iterator::operator>(const iterator &lhs, const iterator &rhs) |
750 | \fn bool QJsonArray::iterator::operator>(const iterator &lhs, const const_iterator &rhs) |
751 | |
752 | Returns \c true if the item pointed to by \a lhs iterator is greater |
753 | than the item pointed to by the \a rhs iterator. |
754 | */ |
755 | |
756 | /*! |
757 | \fn bool QJsonArray::iterator::operator>=(const iterator &lhs, const iterator &rhs) |
758 | \fn bool QJsonArray::iterator::operator>=(const iterator &lhs, const const_iterator &rhs) |
759 | |
760 | Returns \c true if the item pointed to by \a lhs iterator is greater |
761 | than or equal to the item pointed to by the \a rhs iterator. |
762 | */ |
763 | |
764 | /*! \fn QJsonArray::iterator &QJsonArray::iterator::operator++() |
765 | |
766 | The prefix \c{++} operator, \c{++it}, advances the iterator to the |
767 | next item in the array and returns an iterator to the new current |
768 | item. |
769 | |
770 | Calling this function on QJsonArray::end() leads to undefined results. |
771 | |
772 | \sa operator--() |
773 | */ |
774 | |
775 | /*! \fn QJsonArray::iterator QJsonArray::iterator::operator++(int) |
776 | |
777 | \overload |
778 | |
779 | The postfix \c{++} operator, \c{it++}, advances the iterator to the |
780 | next item in the array and returns an iterator to the previously |
781 | current item. |
782 | */ |
783 | |
784 | /*! \fn QJsonArray::iterator &QJsonArray::iterator::operator--() |
785 | |
786 | The prefix \c{--} operator, \c{--it}, makes the preceding item |
787 | current and returns an iterator to the new current item. |
788 | |
789 | Calling this function on QJsonArray::begin() leads to undefined results. |
790 | |
791 | \sa operator++() |
792 | */ |
793 | |
794 | /*! \fn QJsonArray::iterator QJsonArray::iterator::operator--(int) |
795 | |
796 | \overload |
797 | |
798 | The postfix \c{--} operator, \c{it--}, makes the preceding item |
799 | current and returns an iterator to the previously current item. |
800 | */ |
801 | |
802 | /*! \fn QJsonArray::iterator &QJsonArray::iterator::operator+=(qsizetype j) |
803 | |
804 | Advances the iterator by \a j items. If \a j is negative, the |
805 | iterator goes backward. |
806 | |
807 | \sa operator-=(), operator+() |
808 | */ |
809 | |
810 | /*! \fn QJsonArray::iterator &QJsonArray::iterator::operator-=(qsizetype j) |
811 | |
812 | Makes the iterator go back by \a j items. If \a j is negative, |
813 | the iterator goes forward. |
814 | |
815 | \sa operator+=(), operator-() |
816 | */ |
817 | |
818 | /*! \fn QJsonArray::iterator QJsonArray::iterator::operator+(qsizetype j) const |
819 | |
820 | Returns an iterator to the item at \a j positions forward from |
821 | this iterator. If \a j is negative, the iterator goes backward. |
822 | |
823 | \sa operator-(), operator+=() |
824 | */ |
825 | |
826 | /*! \fn QJsonArray::iterator QJsonArray::iterator::operator-(qsizetype j) const |
827 | |
828 | Returns an iterator to the item at \a j positions backward from |
829 | this iterator. If \a j is negative, the iterator goes forward. |
830 | |
831 | \sa operator+(), operator-=() |
832 | */ |
833 | |
834 | /*! \fn qsizetype QJsonArray::iterator::operator-(iterator other) const |
835 | |
836 | Returns the number of items between the item pointed to by \a |
837 | other and the item pointed to by this iterator. |
838 | */ |
839 | |
840 | /*! \class QJsonArray::const_iterator |
841 | \inmodule QtCore |
842 | \brief The QJsonArray::const_iterator class provides an STL-style const iterator for QJsonArray. |
843 | |
844 | \compares strong |
845 | \compareswith strong QJsonArray::iterator |
846 | \endcompareswith |
847 | |
848 | QJsonArray::const_iterator allows you to iterate over a |
849 | QJsonArray. If you want to modify the QJsonArray as |
850 | you iterate over it, use QJsonArray::iterator instead. It is generally a |
851 | good practice to use QJsonArray::const_iterator on a non-const QJsonArray |
852 | as well, unless you need to change the QJsonArray through the |
853 | iterator. Const iterators are slightly faster and improves |
854 | code readability. |
855 | |
856 | The default QJsonArray::const_iterator constructor creates an |
857 | uninitialized iterator. You must initialize it using a QJsonArray |
858 | function like QJsonArray::constBegin(), QJsonArray::constEnd(), or |
859 | QJsonArray::insert() before you can start iterating. |
860 | |
861 | Most QJsonArray functions accept an integer index rather than an |
862 | iterator. For that reason, iterators are rarely useful in |
863 | connection with QJsonArray. One place where STL-style iterators do |
864 | make sense is as arguments to \l{generic algorithms}. |
865 | |
866 | Multiple iterators can be used on the same array. However, be |
867 | aware that any non-const function call performed on the QJsonArray |
868 | will render all existing iterators undefined. |
869 | |
870 | \sa QJsonArray::iterator |
871 | */ |
872 | |
873 | /*! \fn QJsonArray::const_iterator::const_iterator() |
874 | |
875 | Constructs an uninitialized iterator. |
876 | |
877 | Functions like operator*() and operator++() should not be called |
878 | on an uninitialized iterator. Use operator=() to assign a value |
879 | to it before using it. |
880 | |
881 | \sa QJsonArray::constBegin(), QJsonArray::constEnd() |
882 | */ |
883 | |
884 | /*! \fn QJsonArray::const_iterator::const_iterator(const QJsonArray *array, qsizetype index) |
885 | \internal |
886 | */ |
887 | |
888 | /*! \typedef QJsonArray::const_iterator::iterator_category |
889 | |
890 | A synonym for \e {std::random_access_iterator_tag} indicating |
891 | this iterator is a random access iterator. |
892 | */ |
893 | |
894 | /*! \typedef QJsonArray::const_iterator::difference_type |
895 | |
896 | \internal |
897 | */ |
898 | |
899 | /*! \typedef QJsonArray::const_iterator::value_type |
900 | |
901 | \internal |
902 | */ |
903 | |
904 | /*! \typedef QJsonArray::const_iterator::reference |
905 | |
906 | \internal |
907 | */ |
908 | |
909 | /*! \typedef QJsonArray::const_iterator::pointer |
910 | |
911 | \internal |
912 | */ |
913 | |
914 | /*! \fn QJsonArray::const_iterator::const_iterator(const iterator &other) |
915 | |
916 | Constructs a copy of \a other. |
917 | */ |
918 | |
919 | /*! \fn const QJsonValueRef QJsonArray::const_iterator::operator*() const |
920 | |
921 | Returns the current item. |
922 | */ |
923 | |
924 | /*! \fn const QJsonValueRef *QJsonArray::const_iterator::operator->() const |
925 | |
926 | Returns a pointer to the current item. |
927 | */ |
928 | |
929 | /*! \fn QJsonValue QJsonArray::const_iterator::operator[](qsizetype j) const |
930 | |
931 | Returns the item at offset \a j from the item pointed to by this iterator (the item at |
932 | position \c{*this + j}). |
933 | |
934 | This function is provided to make QJsonArray iterators behave like C++ |
935 | pointers. |
936 | |
937 | \sa operator+() |
938 | */ |
939 | |
940 | /*! \fn bool QJsonArray::const_iterator::operator==(const const_iterator &lhs, const const_iterator &rhs) |
941 | |
942 | Returns \c true if \a lhs points to the same item as \a rhs |
943 | iterator; otherwise returns \c false. |
944 | |
945 | \sa operator!=() |
946 | */ |
947 | |
948 | /*! \fn bool QJsonArray::const_iterator::operator!=(const const_iterator &lhs, const const_iterator &rhs) |
949 | |
950 | Returns \c true if \a lhs points to a different item than \a rhs |
951 | iterator; otherwise returns \c false. |
952 | |
953 | \sa operator==() |
954 | */ |
955 | |
956 | /*! |
957 | \fn bool QJsonArray::const_iterator::operator<(const const_iterator &lhs, const const_iterator &rhs) |
958 | |
959 | Returns \c true if the item pointed to by \a lhs iterator is less than |
960 | the item pointed to by the \a rhs iterator. |
961 | */ |
962 | |
963 | /*! |
964 | \fn bool QJsonArray::const_iterator::operator<=(const const_iterator &lhs, const const_iterator &rhs) |
965 | |
966 | Returns \c true if the item pointed to by \a lhs iterator is less than |
967 | or equal to the item pointed to by the \a rhs iterator. |
968 | */ |
969 | |
970 | /*! |
971 | \fn bool QJsonArray::const_iterator::operator>(const const_iterator &lhs, const const_iterator &rhs) |
972 | |
973 | Returns \c true if the item pointed to by \a lhs iterator is greater |
974 | than the item pointed to by the \a rhs iterator. |
975 | */ |
976 | |
977 | /*! |
978 | \fn bool QJsonArray::const_iterator::operator>=(const const_iterator &lhs, const const_iterator &rhs) |
979 | |
980 | Returns \c true if the item pointed to by \a lhs iterator is greater |
981 | than or equal to the item pointed to by the \a rhs iterator. |
982 | */ |
983 | |
984 | /*! \fn QJsonArray::const_iterator &QJsonArray::const_iterator::operator++() |
985 | |
986 | The prefix \c{++} operator, \c{++it}, advances the iterator to the |
987 | next item in the array and returns an iterator to the new current |
988 | item. |
989 | |
990 | Calling this function on QJsonArray::end() leads to undefined results. |
991 | |
992 | \sa operator--() |
993 | */ |
994 | |
995 | /*! \fn QJsonArray::const_iterator QJsonArray::const_iterator::operator++(int) |
996 | |
997 | \overload |
998 | |
999 | The postfix \c{++} operator, \c{it++}, advances the iterator to the |
1000 | next item in the array and returns an iterator to the previously |
1001 | current item. |
1002 | */ |
1003 | |
1004 | /*! \fn QJsonArray::const_iterator &QJsonArray::const_iterator::operator--() |
1005 | |
1006 | The prefix \c{--} operator, \c{--it}, makes the preceding item |
1007 | current and returns an iterator to the new current item. |
1008 | |
1009 | Calling this function on QJsonArray::begin() leads to undefined results. |
1010 | |
1011 | \sa operator++() |
1012 | */ |
1013 | |
1014 | /*! \fn QJsonArray::const_iterator QJsonArray::const_iterator::operator--(int) |
1015 | |
1016 | \overload |
1017 | |
1018 | The postfix \c{--} operator, \c{it--}, makes the preceding item |
1019 | current and returns an iterator to the previously current item. |
1020 | */ |
1021 | |
1022 | /*! \fn QJsonArray::const_iterator &QJsonArray::const_iterator::operator+=(qsizetype j) |
1023 | |
1024 | Advances the iterator by \a j items. If \a j is negative, the |
1025 | iterator goes backward. |
1026 | |
1027 | \sa operator-=(), operator+() |
1028 | */ |
1029 | |
1030 | /*! \fn QJsonArray::const_iterator &QJsonArray::const_iterator::operator-=(qsizetype j) |
1031 | |
1032 | Makes the iterator go back by \a j items. If \a j is negative, |
1033 | the iterator goes forward. |
1034 | |
1035 | \sa operator+=(), operator-() |
1036 | */ |
1037 | |
1038 | /*! \fn QJsonArray::const_iterator QJsonArray::const_iterator::operator+(qsizetype j) const |
1039 | |
1040 | Returns an iterator to the item at \a j positions forward from |
1041 | this iterator. If \a j is negative, the iterator goes backward. |
1042 | |
1043 | \sa operator-(), operator+=() |
1044 | */ |
1045 | |
1046 | /*! \fn QJsonArray::const_iterator QJsonArray::const_iterator::operator-(qsizetype j) const |
1047 | |
1048 | Returns an iterator to the item at \a j positions backward from |
1049 | this iterator. If \a j is negative, the iterator goes forward. |
1050 | |
1051 | \sa operator+(), operator-=() |
1052 | */ |
1053 | |
1054 | /*! \fn qsizetype QJsonArray::const_iterator::operator-(const_iterator other) const |
1055 | |
1056 | Returns the number of items between the item pointed to by \a |
1057 | other and the item pointed to by this iterator. |
1058 | */ |
1059 | |
1060 | /*! |
1061 | \internal |
1062 | */ |
1063 | bool QJsonArray::detach(qsizetype reserve) |
1064 | { |
1065 | if (!a) |
1066 | return true; |
1067 | a = a->detach(d: a.data(), reserved: reserve ? reserve : size()); |
1068 | return a; |
1069 | } |
1070 | |
1071 | size_t qHash(const QJsonArray &array, size_t seed) |
1072 | { |
1073 | return qHashRange(first: array.begin(), last: array.end(), seed); |
1074 | } |
1075 | |
1076 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) |
1077 | QDebug operator<<(QDebug dbg, const QJsonArray &a) |
1078 | { |
1079 | QDebugStateSaver saver(dbg); |
1080 | if (!a.a) { |
1081 | dbg << "QJsonArray()" ; |
1082 | return dbg; |
1083 | } |
1084 | QByteArray json; |
1085 | QJsonPrivate::Writer::arrayToJson(a: a.a.data(), json, indent: 0, compact: true); |
1086 | dbg.nospace() << "QJsonArray(" |
1087 | << json.constData() // print as utf-8 string without extra quotation marks |
1088 | << ")" ; |
1089 | return dbg; |
1090 | } |
1091 | #endif |
1092 | |
1093 | #ifndef QT_NO_DATASTREAM |
1094 | QDataStream &operator<<(QDataStream &stream, const QJsonArray &array) |
1095 | { |
1096 | return stream << QJsonDocument{array}; |
1097 | } |
1098 | |
1099 | QDataStream &operator>>(QDataStream &stream, QJsonArray &array) |
1100 | { |
1101 | QJsonDocument doc; |
1102 | stream >> doc; |
1103 | array = doc.array(); |
1104 | return stream; |
1105 | } |
1106 | #endif |
1107 | |
1108 | QT_END_NAMESPACE |
1109 | |
1110 | |