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

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

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