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