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