Qt 6.x
The Qt SDK
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
103QJsonArray::QJsonArray() = default;
104
121 : a(array)
122{
123}
124
128QJsonArray::~QJsonArray() = default;
129
130QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args)
131{
132 for (const auto & arg : args)
133 append(arg);
134}
135
142QJsonArray::QJsonArray(const QJsonArray &other) noexcept = default;
143
150 : a(other.a)
151{
152 other.a = nullptr;
153}
154
158QJsonArray &QJsonArray::operator =(const QJsonArray &other) noexcept = default;
159
207{
209 for (QStringList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it)
210 array.append(QJsonValue(*it));
211 return array;
212}
213
225{
227}
228
235{
237}
238
239
244{
245 return a ? a->elements.size() : 0;
246}
247
262{
263 return a == nullptr || a->elements.isEmpty();
264}
265
273{
274 if (!a || i < 0 || i >= a->elements.size())
276
277 return QJsonPrivate::Value::fromTrustedCbor(a->valueAt(i));
278}
279
288{
289 return at(0);
290}
291
300{
301 return at(a ? (a->elements.size() - 1) : 0);
302}
303
312{
313 insert(0, value);
314}
315
322{
323 insert(a ? a->elements.size() : 0, value);
324}
325
333{
334 if (!a || i < 0 || i >= a->elements.size())
335 return;
336 detach();
337 a->removeAt(i);
338}
339
369{
370 if (!a || i < 0 || i >= a->elements.size())
372
373 detach();
375 a->removeAt(i);
376 return v;
377}
378
387{
388 if (a)
389 detach(a->elements.size() + 1);
390 else
392
393 Q_ASSERT (i >= 0 && i <= a->elements.size());
394 a->insertAt(i, value.type() == QJsonValue::Undefined ? QCborValue(nullptr)
396}
397
423{
424 Q_ASSERT (a && i >= 0 && i < a->elements.size());
425 detach();
426 a->replaceAt(i, QCborValue::fromJsonValue(value));
427}
428
435{
436 for (qsizetype i = 0; i < size(); i++) {
437 if (at(i) == value)
438 return true;
439 }
440 return false;
441}
442
457{
458 Q_ASSERT(a && i >= 0 && i < a->elements.size());
459 return QJsonValueRef(this, i);
460}
461
468{
469 return at(i);
470}
471
476{
477 if (a == other.a)
478 return true;
479
480 if (!a)
481 return !other.a->elements.size();
482 if (!other.a)
483 return !a->elements.size();
484 if (a->elements.size() != other.a->elements.size())
485 return false;
486
487 for (qsizetype i = 0; i < a->elements.size(); ++i) {
488 if (a->valueAt(i) != other.a->valueAt(i))
489 return false;
490 }
491 return true;
492}
493
498{
499 return !(*this == other);
500}
501
1046bool QJsonArray::detach(qsizetype reserve)
1047{
1048 if (!a)
1049 return true;
1050 a = a->detach(a.data(), reserve ? reserve : size());
1051 return a;
1052}
1053
1054size_t qHash(const QJsonArray &array, size_t seed)
1055{
1056 return qHashRange(array.begin(), array.end(), seed);
1057}
1058
1059#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
1061{
1062 QDebugStateSaver saver(dbg);
1063 if (!a.a) {
1064 dbg << "QJsonArray()";
1065 return dbg;
1066 }
1067 QByteArray json;
1068 QJsonPrivate::Writer::arrayToJson(a.a.data(), json, 0, true);
1069 dbg.nospace() << "QJsonArray("
1070 << json.constData() // print as utf-8 string without extra quotation marks
1071 << ")";
1072 return dbg;
1073}
1074#endif
1075
1076#ifndef QT_NO_DATASTREAM
1078{
1079 QJsonDocument doc{array};
1081 return stream;
1082}
1083
1085{
1086 QJsonDocument doc;
1087 stream >> doc;
1088 array = doc.array();
1089 return stream;
1090}
1091#endif
1092
1094
\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:122
QVariantList toVariantList() const
Recursively converts each \l QCborValue in this array using QCborValue::toVariant() and returns the Q...
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:50
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:30
\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.
bool operator!=(const QJsonArray &other) const
Returns true if this array is not equal to other.
QJsonArray()
Creates an empty array.
friend class QJsonValueRef
Definition qjsonarray.h:222
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.
bool operator==(const QJsonArray &other) const
Returns true if this array is equal to other.
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:220
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
QByteArray toJson(JsonFormat format=Indented) const
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:24
const_iterator constBegin() const noexcept
Definition qlist.h:615
const_iterator constEnd() const noexcept
Definition qlist.h:616
\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)
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:70
QList< int > list
[14]
QSharedPointer< T > other(t)
[5]
QAction * at
QJSValueList args