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

source code of qtbase/src/corelib/serialization/qjsonarray.cpp