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 Class Reference

\inmodule QtCore\reentrant More...

#include <qjsonarray.h>

+ Collaboration diagram for QJsonArray:

Classes

class  const_iterator
 \inmodule QtCore More...
 
class  iterator
 \inmodule QtCore More...
 

Public Types

typedef iterator Iterator
 Qt-style synonym for QJsonArray::iterator.
 
typedef const_iterator ConstIterator
 Qt-style synonym for QJsonArray::const_iterator.
 
typedef qsizetype size_type
 Typedef for qsizetype.
 
typedef QJsonValue value_type
 Typedef for QJsonValue.
 
typedef value_typepointer
 Typedef for QJsonValue *.
 
typedef const value_typeconst_pointer
 Typedef for const QJsonValue *.
 
typedef QJsonValueRef reference
 Typedef for QJsonValue &.
 
typedef QJsonValue const_reference
 Typedef for const QJsonValue &.
 
typedef qsizetype difference_type
 Typedef for qsizetype.
 

Public Member Functions

 QJsonArray ()
 Creates an empty array.
 
 QJsonArray (std::initializer_list< QJsonValue > args)
 
 ~QJsonArray ()
 Deletes the array.
 
 QJsonArray (const QJsonArray &other) noexcept
 Creates a copy of other.
 
QJsonArrayoperator= (const QJsonArray &other) noexcept
 Assigns other to this array.
 
 QJsonArray (QJsonArray &&other) noexcept
 
QJsonArrayoperator= (QJsonArray &&other) noexcept
 
QVariantList toVariantList () const
 Converts this object to a QVariantList.
 
qsizetype size () const
 Returns the number of values stored in the array.
 
qsizetype count () const
 Same as size().
 
bool isEmpty () const
 Returns true if the object is empty.
 
QJsonValue at (qsizetype i) const
 Returns a QJsonValue representing the value for index i.
 
QJsonValue first () const
 Returns the first value stored in the array.
 
QJsonValue last () const
 Returns the last value stored in the array.
 
void prepend (const QJsonValue &value)
 Inserts value at the beginning of the array.
 
void append (const QJsonValue &value)
 Inserts value at the end of the array.
 
void removeAt (qsizetype i)
 Removes the value at index position i.
 
QJsonValue takeAt (qsizetype i)
 Removes the item at index position i and returns it.
 
void removeFirst ()
 Removes the first item in the array.
 
void removeLast ()
 Removes the last item in the array.
 
void insert (qsizetype i, const QJsonValue &value)
 Inserts value at index position i in the array.
 
void replace (qsizetype i, const QJsonValue &value)
 Replaces the item at index position i with value.
 
bool contains (const QJsonValue &element) const
 Returns true if the array contains an occurrence of value, otherwise false.
 
QJsonValueRef operator[] (qsizetype i)
 Returns the value at index position i as a modifiable reference.
 
QJsonValue operator[] (qsizetype i) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as at().
 
void swap (QJsonArray &other) noexcept
 
iterator begin ()
 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.
 
const_iterator begin () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const_iterator constBegin () const
 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.
 
const_iterator cbegin () const
 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.
 
iterator end ()
 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the array.
 
const_iterator end () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const_iterator constEnd () const
 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the array.
 
const_iterator cend () const
 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the array.
 
iterator insert (iterator before, const QJsonValue &value)
 Inserts value before the position pointed to by before, and returns an iterator pointing to the newly inserted item.
 
iterator erase (iterator it)
 Removes the item pointed to by it, and returns an iterator pointing to the next item.
 
QJsonArray operator+ (const QJsonValue &v) const
 Returns an array that contains all the items in this array followed by the provided value.
 
QJsonArrayoperator+= (const QJsonValue &v)
 Appends value to the array, and returns a reference to the array itself.
 
QJsonArrayoperator<< (const QJsonValue &v)
 Appends value to the array, and returns a reference to the array itself.
 
void push_back (const QJsonValue &t)
 This function is provided for STL compatibility.
 
void push_front (const QJsonValue &t)
 This function is provided for STL compatibility.
 
void pop_front ()
 This function is provided for STL compatibility.
 
void pop_back ()
 This function is provided for STL compatibility.
 
bool empty () const
 This function is provided for STL compatibility.
 

Static Public Member Functions

static QJsonArray fromStringList (const QStringList &list)
 Converts the string list list to a QJsonArray.
 
static QJsonArray fromVariantList (const QVariantList &list)
 Converts the variant list list to a QJsonArray.
 

Friends

class iterator
 
class const_iterator
 
class QJsonValue
 
class QJsonValueConstRef
 
class QJsonValueRef
 
class QJsonPrivate::Value
 
class QJsonDocument
 
class QCborArray
 
Q_CORE_EXPORT QDebug operator<< (QDebug, const QJsonArray &)
 
Q_CORE_EXPORT bool comparesEqual (const QJsonArray &lhs, const QJsonArray &rhs) noexcept
 
Q_CORE_EXPORT bool comparesEqual (const QJsonArray &lhs, const QJsonValue &rhs) noexcept
 

Detailed Description

\inmodule QtCore

\reentrant

Since
5.0

The QJsonArray class encapsulates a JSON array.

\compares equality \compareswith equality QJsonValue \endcompareswith

A JSON array is a list of values. The list can be manipulated by inserting and removing QJsonValue's from the array.

A QJsonArray can be converted to and from a QVariantList. You can query the number of entries with size(), insert(), and removeAt() entries from it and iterate over its content using the standard C++ iterator pattern.

QJsonArray is an implicitly shared class and shares the data with the document it has been created from as long as it is not being modified.

You can convert the array to and from text based JSON through QJsonDocument.

See also
{JSON Support in Qt}, {Saving and Loading a Game}

Definition at line 17 of file qjsonarray.h.

Member Typedef Documentation

◆ const_pointer

Typedef for const QJsonValue *.

Provided for STL compatibility.

Definition at line 290 of file qjsonarray.h.

◆ const_reference

Typedef for const QJsonValue &.

Provided for STL compatibility.

Definition at line 292 of file qjsonarray.h.

◆ ConstIterator

Qt-style synonym for QJsonArray::const_iterator.

Definition at line 271 of file qjsonarray.h.

◆ difference_type

Typedef for qsizetype.

Provided for STL compatibility.

Definition at line 293 of file qjsonarray.h.

◆ Iterator

Qt-style synonym for QJsonArray::iterator.

Definition at line 270 of file qjsonarray.h.

◆ pointer

Typedef for QJsonValue *.

Provided for STL compatibility.

Definition at line 289 of file qjsonarray.h.

◆ reference

Typedef for QJsonValue &.

Provided for STL compatibility.

Definition at line 291 of file qjsonarray.h.

◆ size_type

Typedef for qsizetype.

Provided for STL compatibility.

Definition at line 287 of file qjsonarray.h.

◆ value_type

Typedef for QJsonValue.

Provided for STL compatibility.

Definition at line 288 of file qjsonarray.h.

Constructor & Destructor Documentation

◆ QJsonArray() [1/4]

QJsonArray::QJsonArray ( )
default

Creates an empty array.

◆ QJsonArray() [2/4]

QJsonArray::QJsonArray ( std::initializer_list< QJsonValue > args)
Since
5.4 Creates an array initialized from args initialization list.

QJsonArray can be constructed in a way similar to JSON notation, for example:

QJsonArray array = { 1, 2.2, QString() };
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
GLenum array

Definition at line 134 of file qjsonarray.cpp.

References append(), arg, and args.

+ Here is the call graph for this function:

◆ ~QJsonArray()

QJsonArray::~QJsonArray ( )
default

Deletes the array.

◆ QJsonArray() [3/4]

QJsonArray::QJsonArray ( const QJsonArray & other)
defaultnoexcept

Creates a copy of other.

Since QJsonArray is implicitly shared, the copy is shallow as long as the object doesn't get modified.

◆ QJsonArray() [4/4]

QJsonArray::QJsonArray ( QJsonArray && other)
noexcept
Since
5.10

Move-constructs a QJsonArray from other.

Definition at line 153 of file qjsonarray.cpp.

References other().

+ Here is the call graph for this function:

Member Function Documentation

◆ append()

void QJsonArray::append ( const QJsonValue & value)

Inserts value at the end of the array.

See also
prepend(), insert()

Definition at line 327 of file qjsonarray.cpp.

References insert().

Referenced by QJsonArray(), addDeco(), blockMemberObject(), builtinObject(), convertToJsonArray(), fromStringList(), Moc::generate(), inOutObject(), QQmlJSLinter::lintFile(), QQmlJSLinter::lintModule(), QV4DebugClient::lookup(), QShaderDescriptionPrivate::makeDoc(), QmlLsp::messageToDiagnostic_helper(), QV4DebugClient::scripts(), ClassDef::toJson(), and EnumDef::toJson().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ at()

QJsonValue QJsonArray::at ( qsizetype i) const

Returns a QJsonValue representing the value for index i.

The returned QJsonValue is Undefined, if i is out of bounds.

Definition at line 278 of file qjsonarray.cpp.

References QJsonPrivate::Value::fromTrustedCbor(), i, QJsonValue, and QJsonValue::Undefined.

Referenced by constructRoute(), QDefaultOutputMapping::load(), QKmsScreenConfig::loadConfig(), QJsonValue::operator[](), parseContactDetails(), and QGeoRouteParserOsrmV5Private::parseReply().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ begin() [1/2]

QJsonArray::iterator QJsonArray::begin ( )
inline

Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.

See also
constBegin(), end()

Definition at line 256 of file qjsonarray.h.

◆ begin() [2/2]

QJsonArray::const_iterator QJsonArray::begin ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 257 of file qjsonarray.h.

◆ cbegin()

QJsonArray::const_iterator QJsonArray::cbegin ( ) const
inline

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.

See also
begin(), cend()

Definition at line 259 of file qjsonarray.h.

◆ cend()

QJsonArray::const_iterator QJsonArray::cend ( ) const
inline

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the array.

See also
cbegin(), end()

Definition at line 263 of file qjsonarray.h.

◆ constBegin()

QJsonArray::const_iterator QJsonArray::constBegin ( ) const
inline

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the array.

See also
begin(), constEnd()

Definition at line 258 of file qjsonarray.h.

◆ constEnd()

QJsonArray::const_iterator QJsonArray::constEnd ( ) const
inline

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the array.

See also
constBegin(), end()

Definition at line 262 of file qjsonarray.h.

◆ contains()

bool QJsonArray::contains ( const QJsonValue & value) const

Returns true if the array contains an occurrence of value, otherwise false.

See also
count()

Definition at line 440 of file qjsonarray.cpp.

References at, and i.

◆ count()

QJsonArray::count ( ) const
inline

Same as size().

See also
size()

Definition at line 42 of file qjsonarray.h.

Referenced by constructRoute(), parseContactDetails(), and runQmlImportScanner().

+ Here is the caller graph for this function:

◆ empty()

bool QJsonArray::empty ( ) const
inline

This function is provided for STL compatibility.

It is equivalent to isEmpty() and returns true if the array is empty.

Definition at line 286 of file qjsonarray.h.

◆ end() [1/2]

QJsonArray::iterator QJsonArray::end ( )
inline

Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the array.

See also
begin(), constEnd()

Definition at line 260 of file qjsonarray.h.

◆ end() [2/2]

const_iterator QJsonArray::end ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 261 of file qjsonarray.h.

◆ erase()

QJsonArray::iterator QJsonArray::erase ( iterator it)
inline

Removes the item pointed to by it, and returns an iterator pointing to the next item.

See also
removeAt()

Definition at line 266 of file qjsonarray.h.

References it.

◆ first()

QJsonValue QJsonArray::first ( ) const

Returns the first value stored in the array.

Same as at(0).

See also
at()

Definition at line 293 of file qjsonarray.cpp.

References at.

◆ fromStringList()

QJsonArray QJsonArray::fromStringList ( const QStringList & list)
static

Converts the string list list to a QJsonArray.

The values in list will be converted to JSON values.

See also
toVariantList(), QJsonValue::fromVariant()

Definition at line 210 of file qjsonarray.cpp.

References append(), QList< T >::constBegin(), QList< T >::constEnd(), it, and list.

Referenced by QJsonValue::fromVariant().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fromVariantList()

QJsonArray QJsonArray::fromVariantList ( const QVariantList & list)
static

Converts the variant list list to a QJsonArray.

The QVariant values in list will be converted to JSON values.

Note
Conversion from \l QVariant is not completely lossless. Please see the documentation in QJsonValue::fromVariant() for more information.
See also
toVariantList(), QJsonValue::fromVariant()

Definition at line 229 of file qjsonarray.cpp.

References list, and QJsonPrivate::Variant::toJsonArray().

Referenced by QJsonDocument::fromVariant(), and QJsonValue::fromVariant().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insert() [1/2]

QJsonArray::iterator QJsonArray::insert ( iterator before,
const QJsonValue & value )
inline

Inserts value before the position pointed to by before, and returns an iterator pointing to the newly inserted item.

See also
erase(), insert()

Definition at line 264 of file qjsonarray.h.

References insert().

+ Here is the call graph for this function:

◆ insert() [2/2]

void QJsonArray::insert ( qsizetype i,
const QJsonValue & value )

Inserts value at index position i in the array.

If i is 0, the value is prepended to the array. If i is size(), the value is appended to the array.

See also
append(), prepend(), replace(), removeAt()

Definition at line 392 of file qjsonarray.cpp.

References elements, QCborValue::fromJsonValue(), i, Q_ASSERT, and QJsonValue::Undefined.

Referenced by append(), prepend(), and readGpuFeatures().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isEmpty()

bool QJsonArray::isEmpty ( ) const

Returns true if the object is empty.

This is the same as size() == 0.

See also
size()

Definition at line 267 of file qjsonarray.cpp.

Referenced by ClassDef::toJson().

+ Here is the caller graph for this function:

◆ last()

QJsonValue QJsonArray::last ( ) const

Returns the last value stored in the array.

Same as {at(size() - 1)}.

See also
at()

Definition at line 305 of file qjsonarray.cpp.

References at.

◆ operator+()

QJsonArray QJsonArray::operator+ ( const QJsonValue & value) const
inline

Returns an array that contains all the items in this array followed by the provided value.

Since
5.3
See also
operator+=()

Definition at line 274 of file qjsonarray.h.

◆ operator+=()

QJsonArray & QJsonArray::operator+= ( const QJsonValue & value)
inline

Appends value to the array, and returns a reference to the array itself.

Since
5.3
See also
append(), operator<<()

Definition at line 276 of file qjsonarray.h.

References append().

+ Here is the call graph for this function:

◆ operator<<()

QJsonArray & QJsonArray::operator<< ( const QJsonValue & value)
inline

Appends value to the array, and returns a reference to the array itself.

Since
5.3
See also
operator+=(), append()

Definition at line 278 of file qjsonarray.h.

References append().

+ Here is the call graph for this function:

◆ operator=() [1/2]

QJsonArray & QJsonArray::operator= ( const QJsonArray & other)
defaultnoexcept

Assigns other to this array.

◆ operator=() [2/2]

QJsonArray & QJsonArray::operator= ( QJsonArray && other)
inlinenoexcept
Since
5.10

Move-assigns other to this array.

Definition at line 31 of file qjsonarray.h.

References other(), and swap().

+ Here is the call graph for this function:

◆ operator[]() [1/2]

QJsonValueRef QJsonArray::operator[] ( qsizetype i)

Returns the value at index position i as a modifiable reference.

i must be a valid index position in the array (i.e., {0 <= i < size()}).

The return value is of type QJsonValueRef, a helper class for QJsonArray and QJsonObject. When you get an object of type QJsonValueRef, you can use it as if it were a reference to a QJsonValue. If you assign to it, the assignment will apply to the character in the QJsonArray of QJsonObject from which you got the reference.

See also
at()

Definition at line 462 of file qjsonarray.cpp.

References elements, i, Q_ASSERT, and QJsonValueRef.

◆ operator[]() [2/2]

QJsonValue QJsonArray::operator[] ( qsizetype i) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as at().

Definition at line 473 of file qjsonarray.cpp.

References at, and i.

◆ pop_back()

void QJsonArray::pop_back ( )
inline

This function is provided for STL compatibility.

It is equivalent to removeLast(). The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

Definition at line 285 of file qjsonarray.h.

◆ pop_front()

void QJsonArray::pop_front ( )
inline

This function is provided for STL compatibility.

It is equivalent to removeFirst(). The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

Definition at line 284 of file qjsonarray.h.

◆ prepend()

void QJsonArray::prepend ( const QJsonValue & value)

Inserts value at the beginning of the array.

This is the same as {insert(0, value)} and will prepend value to the array.

See also
append(), insert()

Definition at line 317 of file qjsonarray.cpp.

References insert().

+ Here is the call graph for this function:

◆ push_back()

void QJsonArray::push_back ( const QJsonValue & value)
inline

This function is provided for STL compatibility.

It is equivalent to \l{QJsonArray::append()}{append(value)} and will append value to the array.

Definition at line 282 of file qjsonarray.h.

References append().

+ Here is the call graph for this function:

◆ push_front()

void QJsonArray::push_front ( const QJsonValue & value)
inline

This function is provided for STL compatibility.

It is equivalent to \l{QJsonArray::prepend()}{prepend(value)} and will prepend value to the array.

Definition at line 283 of file qjsonarray.h.

References prepend().

+ Here is the call graph for this function:

◆ removeAt()

void QJsonArray::removeAt ( qsizetype i)

Removes the value at index position i.

i must be a valid index position in the array (i.e., {0 <= i < size()}).

See also
insert(), replace()

Definition at line 338 of file qjsonarray.cpp.

References i.

◆ removeFirst()

void QJsonArray::removeFirst ( )
inline

Removes the first item in the array.

Calling this function is equivalent to calling {removeAt(0)}. The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

See also
removeAt(), removeLast()

Definition at line 53 of file qjsonarray.h.

◆ removeLast()

void QJsonArray::removeLast ( )
inline

Removes the last item in the array.

Calling this function is equivalent to calling {removeAt(size() - 1)}. The array must not be empty. If the array can be empty, call isEmpty() before calling this function.

See also
removeAt(), removeFirst()

Definition at line 54 of file qjsonarray.h.

◆ replace()

void QJsonArray::replace ( qsizetype i,
const QJsonValue & value )

Replaces the item at index position i with value.

i must be a valid index position in the array (i.e., {0 <= i < size()}).

See also
operator[](), removeAt()

Definition at line 428 of file qjsonarray.cpp.

References elements, QCborValue::fromJsonValue(), i, and Q_ASSERT.

+ Here is the call graph for this function:

◆ size()

qsizetype QJsonArray::size ( ) const

Returns the number of values stored in the array.

Definition at line 249 of file qjsonarray.cpp.

Referenced by QDefaultOutputMapping::load(), QKmsScreenConfig::loadConfig(), QGeoRouteParserOsrmV5Private::parseReply(), and ClassDef::toJson().

+ Here is the caller graph for this function:

◆ swap()

void QJsonArray::swap ( QJsonArray & other)
inlinenoexcept
Since
5.10

Swaps the array other with this. This operation is very fast and never fails.

Definition at line 67 of file qjsonarray.h.

References other().

+ Here is the call graph for this function:

◆ takeAt()

QJsonValue QJsonArray::takeAt ( qsizetype i)

Removes the item at index position i and returns it.

i must be a valid index position in the array (i.e., {0 <= i < size()}).

If you don't use the return value, removeAt() is more efficient.

See also
removeAt()

Definition at line 374 of file qjsonarray.cpp.

References QJsonPrivate::Value::fromTrustedCbor(), i, QJsonValue, and QJsonValue::Undefined.

+ Here is the call graph for this function:

◆ toVariantList()

QVariantList QJsonArray::toVariantList ( ) const

Converts this object to a QVariantList.

Returns the created map.

Definition at line 239 of file qjsonarray.cpp.

References QCborArray::fromJsonArray().

Referenced by deployQmlImports(), QJsonDocument::toVariant(), and QJsonValue::toVariant().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ comparesEqual [1/2]

Q_CORE_EXPORT bool comparesEqual ( const QJsonArray & lhs,
const QJsonArray & rhs )
friend

Definition at line 478 of file qjsonarray.cpp.

◆ comparesEqual [2/2]

Q_CORE_EXPORT bool comparesEqual ( const QJsonArray & lhs,
const QJsonValue & rhs )
friend

Definition at line 497 of file qjsonarray.cpp.

◆ const_iterator

friend class const_iterator
friend

Definition at line 253 of file qjsonarray.h.

◆ iterator

friend class iterator
friend

Definition at line 178 of file qjsonarray.h.

◆ operator<<

Q_CORE_EXPORT QDebug operator<< ( QDebug ,
const QJsonArray &  )
friend

Definition at line 1078 of file qjsonarray.cpp.

◆ QCborArray

friend class QCborArray
friend

Definition at line 301 of file qjsonarray.h.

◆ QJsonDocument

friend class QJsonDocument
friend

Definition at line 300 of file qjsonarray.h.

◆ QJsonPrivate::Value

friend class QJsonPrivate::Value
friend

Definition at line 299 of file qjsonarray.h.

◆ QJsonValue

friend class QJsonValue
friend

Definition at line 296 of file qjsonarray.h.

Referenced by at(), and takeAt().

◆ QJsonValueConstRef

friend class QJsonValueConstRef
friend

Definition at line 297 of file qjsonarray.h.

◆ QJsonValueRef

friend class QJsonValueRef
friend

Definition at line 298 of file qjsonarray.h.

Referenced by operator[]().


The documentation for this class was generated from the following files: