Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qjsonarray.cpp
Go to the documentation of this file.
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
19
107QJsonArray::QJsonArray() = default;
108
125 : a(array)
126{
127}
128
132QJsonArray::~QJsonArray() = default;
133
134QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args)
135{
136 for (const auto & arg : args)
137 append(arg);
138}
139
146QJsonArray::QJsonArray(const QJsonArray &other) noexcept = default;
147
154 : a(other.a)
155{
156 other.a = nullptr;
157}
158
162QJsonArray &QJsonArray::operator =(const QJsonArray &other) noexcept = default;
163
211{
213 for (QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it)
215 return array;
216}
217
218#ifndef QT_NO_VARIANT
233
240{
241 return QCborArray::fromJsonArray(*this).toVariantList();
242}
243#endif // !QT_NO_VARIANT
244
245
250{
251 return a ? a->elements.size() : 0;
252}
253
268{
269 return a == nullptr || a->elements.isEmpty();
270}
271
279{
280 if (!a || i < 0 || i >= a->elements.size())
282
283 return QJsonPrivate::Value::fromTrustedCbor(a->valueAt(i));
284}
285
294{
295 return at(0);
296}
297
306{
307 return at(a ? (a->elements.size() - 1) : 0);
308}
309
318{
319 insert(0, value);
320}
321
328{
329 insert(a ? a->elements.size() : 0, value);
330}
331
339{
340 if (!a || i < 0 || i >= a->elements.size())
341 return;
342 detach();
343 a->removeAt(i);
344}
345
375{
376 if (!a || i < 0 || i >= a->elements.size())
378
379 detach();
381 a->removeAt(i);
382 return v;
383}
384
393{
394 if (a)
395 detach(a->elements.size() + 1);
396 else
398
399 Q_ASSERT (i >= 0 && i <= a->elements.size());
400 a->insertAt(i, value.type() == QJsonValue::Undefined ? QCborValue(nullptr)
402}
403
429{
430 Q_ASSERT (a && i >= 0 && i < a->elements.size());
431 detach();
432 a->replaceAt(i, QCborValue::fromJsonValue(value));
433}
434
441{
442 for (qsizetype i = 0; i < size(); i++) {
443 if (at(i) == value)
444 return true;
445 }
446 return false;
447}
448
463{
464 Q_ASSERT(a && i >= 0 && i < a->elements.size());
465 return QJsonValueRef(this, i);
466}
467
474{
475 return at(i);
476}
477
478bool comparesEqual(const QJsonArray &lhs, const QJsonArray &rhs) noexcept
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(i) != rhs.a->valueAt(i))
492 return false;
493 }
494 return true;
495}
496
497bool comparesEqual(const QJsonArray &lhs, const QJsonValue &rhs) noexcept
498{
499 return lhs == rhs.toArray();
500}
501
1064bool QJsonArray::detach(qsizetype reserve)
1065{
1066 if (!a)
1067 return true;
1068 a = a->detach(a.data(), reserve ? reserve : size());
1069 return a;
1070}
1071
1072size_t qHash(const QJsonArray &array, size_t seed)
1073{
1074 return qHashRange(array.begin(), array.end(), seed);
1075}
1076
1077#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
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.data(), json, 0, 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
1099
1101{
1102 QJsonDocument doc;
1103 stream >> doc;
1104 array = doc.array();
1105 return stream;
1106}
1107#endif
1108
1110
\inmodule QtCore
Definition qbytearray.h:57
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
static QCborArray fromJsonArray(const QJsonArray &array)
Converts all JSON items found in the array array to CBOR using QCborValue::fromJson(),...
\inmodule QtCore\reentrant
Definition qcborvalue.h:47
static QCborValue fromJsonValue(const QJsonValue &v)
Converts the JSON value contained in v into its corresponding CBOR value and returns it.
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
void insert(qsizetype i, const QJsonValue &value)
Inserts value at index position i in the array.
static QJsonArray fromStringList(const QStringList &list)
Converts the string list list to a QJsonArray.
~QJsonArray()
Deletes the array.
bool contains(const QJsonValue &element) const
Returns true if the array contains an occurrence of value, otherwise false.
static QJsonArray fromVariantList(const QVariantList &list)
Converts the variant list list to a QJsonArray.
QJsonValueRef operator[](qsizetype i)
Returns the value at index position i as a modifiable reference.
bool isEmpty() const
Returns true if the object is empty.
QJsonArray()
Creates an empty array.
friend class QJsonValueRef
Definition qjsonarray.h:298
QJsonValue takeAt(qsizetype i)
Removes the item at index position i and returns it.
QJsonValue first() const
Returns the first value stored in the array.
qsizetype size() const
Returns the number of values stored in the array.
void removeAt(qsizetype i)
Removes the value at index position i.
void replace(qsizetype i, const QJsonValue &value)
Replaces the item at index position i with value.
QJsonArray & operator=(const QJsonArray &other) noexcept
Assigns other to this array.
friend class QJsonValue
Definition qjsonarray.h:296
void append(const QJsonValue &value)
Inserts value at the end of the array.
QJsonValue at(qsizetype i) const
Returns a QJsonValue representing the value for index i.
QVariantList toVariantList() const
Converts this object to a QVariantList.
QJsonValue last() const
Returns the last value stored in the array.
void prepend(const QJsonValue &value)
Inserts value at the beginning of the array.
\inmodule QtCore\reentrant
QJsonArray array() const
Returns the QJsonArray contained in the document.
static QJsonValue fromTrustedCbor(const QCborValue &v)
Definition qjson_p.h:200
static QJsonArray toJsonArray(const QVariantList &list)
static void arrayToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact=false)
\inmodule QtCore \reentrant
\inmodule QtCore\reentrant
Definition qjsonvalue.h:25
const_iterator constBegin() const noexcept
Definition qlist.h:632
const_iterator constEnd() const noexcept
Definition qlist.h:633
\inmodule QtCore
QSet< QString >::iterator it
set reserve(20000)
Combined button and popup list for selecting options.
EGLStreamKHR stream
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
size_t qHashRange(InputIterator first, InputIterator last, size_t seed=0) noexcept(noexcept(qHash(*first)))
QDebug operator<<(QDebug dbg, const QJsonArray &a)
QDataStream & operator>>(QDataStream &stream, QJsonArray &array)
bool comparesEqual(const QJsonArray &lhs, const QJsonArray &rhs) noexcept
size_t qHash(const QJsonArray &array, size_t seed)
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum array
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
SSL_CTX int void * arg
static const QTextHtmlElement elements[Html_NumElements]
ptrdiff_t qsizetype
Definition qtypes.h:165
QList< int > list
[14]
QSharedPointer< T > other(t)
[5]
QAction * at
QJSValueList args