6.0.0-rc4Source

QJsonArray Class

The QJsonArray class encapsulates a JSON array. More…

Header#include <QJsonArray>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QJsonArray()
QJsonArray(std::initializer_list<QJsonValue> args)
QJsonArray(const QJsonArray &other)
QJsonArray(QJsonArray &&other)
~QJsonArray()
QJsonArray &operator=(const QJsonArray &other)
QJsonArray &operator=(QJsonArray &&other)
voidswap(QJsonArray &other)
QVariantListtoVariantList() const
qsizetypesize() const
qsizetypecount() const
qsizetypelength() const
boolisEmpty() const
boolempty() const
QJsonValueat(qsizetype i) const
QJsonValuefirst() const
QJsonValuelast() const
QJsonValueReffirst()
QJsonValueReflast()
voidprepend(const QJsonValue &value)
voidappend(const QJsonValue &value)
voidinsert(qsizetype i, const QJsonValue &value)
voidremoveAt(qsizetype i)
QJsonValuetakeAt(qsizetype i)
voidremoveFirst()
voidremoveLast()
voidreplace(qsizetype i, const QJsonValue &value)
boolcontains(const QJsonValue &element) const
QJsonValueRefoperator[](qsizetype i)
QJsonValueoperator[](qsizetype i) const
QJsonArray &operator+=(const QJsonValue &value)
QJsonArrayoperator+(const QJsonValue &value) const
QJsonArray &operator<<(const QJsonValue &value)
iteratorbegin()
iteratorend()
const_iteratorbegin() const
const_iteratorend() const
const_iteratorcbegin() const
const_iteratorcend() const
const_iteratorconstBegin() const
const_iteratorconstEnd() const
reverse_iteratorrbegin()
reverse_iteratorrend()
const_reverse_iteratorrbegin() const
const_reverse_iteratorrend() const
const_reverse_iteratorcrbegin() const
const_reverse_iteratorcrend() const
iteratorinsert(iterator before, const QJsonValue &value)
iteratorerase(iterator it)
voidpush_back(const QJsonValue &value)
voidpush_front(const QJsonValue &value)
voidpop_front()
voidpop_back()

Static Public Members

QJsonArrayfromStringList(const QStringList &list)
QJsonArrayfromVariantList(const QVariantList &list)

Detailed Description

A JSON array is a list of values. You can append and remove values from the array, and read or replace the value at any index.

QJsonArray array = {1, 2.5, "three"};
array.append(true);
array.insert(0, QJsonValue::Null);
for (const QJsonValue &v : array)
    use(v);

QJsonArray is implicitly shared: copies are cheap and the data is copied when one of them is modified. It is reentrant, so separate arrays can be used from separate threads, but one array must not be modified while another thread reads it or a copy that shares its data.

The iterators are index based. Reading or assigning through an iterator is valid for as long as the array has at least that many elements, and an insertion or removal before the position moves the element the iterator refers to, so, as with QList, treat any iterator at or after a modified position as invalid. Assigning through *it does not invalidate other iterators.

See also QJsonValue, QJsonObject and QJsonDocument.

Differences from Qt

Out-of-range indexes never assert: at() returns an undefined value, and insert(), replace() and removeAt() do nothing. The iterators hold the array and an index instead of a pointer, so copying the array while an iterator is alive is harmless: writes through a non-const iterator go to the array it came from.

Member Type Documentation

typedef QJsonArray::const_pointer

The type const_iterator::operator->() returns, for STL compatibility.

typedef QJsonArray::const_reference

Alias for QJsonValueConstRef, for STL compatibility.

typedef QJsonArray::const_reverse_iterator

A const reverse iterator over the elements, for STL compatibility.

typedef QJsonArray::ConstIterator

Qt-style synonym for QJsonArray::const_iterator.

typedef QJsonArray::difference_type

Alias for qsizetype, for STL compatibility.

typedef QJsonArray::Iterator

Qt-style synonym for QJsonArray::iterator.

typedef QJsonArray::pointer

The type iterator::operator->() returns, for STL compatibility.

typedef QJsonArray::reference

Alias for QJsonValueRef, for STL compatibility.

typedef QJsonArray::reverse_iterator

A reverse iterator over the elements, for STL compatibility.

typedef QJsonArray::size_type

Alias for qsizetype, for STL compatibility.

typedef QJsonArray::value_type

Alias for QJsonValue, for STL compatibility.

Member Function Documentation

QJsonArray::QJsonArray()

Creates an empty array.

QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args)

Creates an array with the values in args.

QJsonArray::QJsonArray(const QJsonArray &other)

Creates a copy of other. The two share their data until one of them is modified.

QJsonArray::QJsonArray(QJsonArray &&other)

Moves other into the new array, which leaves other empty.

QJsonArray::~QJsonArray()

Destroys the array.

void QJsonArray::append(const QJsonValue &value)

Inserts value at the end of the array.

See also prepend() and insert().

QJsonValue QJsonArray::at(qsizetype i) const

Returns the value at index i, or an undefined value if i is out of range.

iterator QJsonArray::begin()

Returns an iterator to the first value.

See also end() and constBegin().

const_iterator QJsonArray::begin() const

This is an overloaded function.

const_iterator QJsonArray::cbegin() const

Returns a const iterator to the first value.

const_iterator QJsonArray::cend() const

Returns a const iterator to the position after the last value.

const_iterator QJsonArray::constBegin() const

Returns a const iterator to the first value.

const_iterator QJsonArray::constEnd() const

Returns a const iterator to the position after the last value.

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

Returns true if the array contains a value equal to element.

qsizetype QJsonArray::count() const

Same as size().

const_reverse_iterator QJsonArray::crbegin() const

Returns a const reverse iterator to the last value.

const_reverse_iterator QJsonArray::crend() const

Returns a const reverse iterator to the position before the first value.

bool QJsonArray::empty() const

Same as isEmpty(), for STL compatibility.

iterator QJsonArray::end()

Returns an iterator to the position after the last value.

See also begin() and constEnd().

const_iterator QJsonArray::end() const

This is an overloaded function.

iterator QJsonArray::erase(iterator it)

Removes the value it refers to and returns an iterator to the value that followed it.

See also insert() and removeAt().

QJsonValue QJsonArray::first() const

Returns the first value, or an undefined value if the array is empty.

See also last() and at().

QJsonValueRef QJsonArray::first()

This is an overloaded function.

Returns a reference to the first value, which can be assigned to.

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

Converts list to an array of strings.

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

Converts list to an array, converting each element with QJsonValue::fromVariant().

See also toVariantList() and QJsonValue::fromVariant().

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

Inserts value at index i, which must be between 0 and size(). Nothing happens for another index.

See also append() and removeAt().

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

Inserts value before the position before and returns an iterator to the new value.

See also erase().

bool QJsonArray::isEmpty() const

Returns true if the array has no values.

QJsonValue QJsonArray::last() const

Returns the last value, or an undefined value if the array is empty.

See also first() and at().

QJsonValueRef QJsonArray::last()

This is an overloaded function.

Returns a reference to the last value, which can be assigned to.

qsizetype QJsonArray::length() const

Same as size().

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

Returns a copy of the array with value appended.

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

Appends value to the array and returns a reference to it.

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

Appends value to the array and returns a reference to it.

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

Assigns other to this array and returns a reference to it.

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

Moves other into this array and returns a reference to it.

QJsonValueRef QJsonArray::operator[](qsizetype i)

Returns a reference to the value at index i, which can be assigned to. Reading through a reference to an index that is out of range gives an undefined value.

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

This is an overloaded function.

Same as at().

void QJsonArray::pop_back()

Same as removeLast(), for STL compatibility.

void QJsonArray::pop_front()

Same as removeFirst(), for STL compatibility.

void QJsonArray::prepend(const QJsonValue &value)

Inserts value at the beginning of the array.

See also append() and insert().

void QJsonArray::push_back(const QJsonValue &value)

Appends value to the array. Same as append(), for STL compatibility.

void QJsonArray::push_front(const QJsonValue &value)

Inserts value at the beginning of the array. Same as prepend(), for STL compatibility.

reverse_iterator QJsonArray::rbegin()

Returns a reverse iterator to the last value.

const_reverse_iterator QJsonArray::rbegin() const

This is an overloaded function.

void QJsonArray::removeAt(qsizetype i)

Removes the value at index i. Nothing happens if i is out of range.

See also takeAt(), removeFirst() and removeLast().

void QJsonArray::removeFirst()

Removes the first value. Nothing happens if the array is empty.

void QJsonArray::removeLast()

Removes the last value. Nothing happens if the array is empty.

reverse_iterator QJsonArray::rend()

Returns a reverse iterator to the position before the first value.

const_reverse_iterator QJsonArray::rend() const

This is an overloaded function.

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

Replaces the value at index i with value. Nothing happens if i is out of range.

See also insert().

qsizetype QJsonArray::size() const

Returns the number of values in the array.

void QJsonArray::swap(QJsonArray &other)

Swaps this array with other.

QJsonValue QJsonArray::takeAt(qsizetype i)

Removes the value at index i and returns it, or returns an undefined value if i is out of range.

See also removeAt().

QVariantList QJsonArray::toVariantList() const

Converts the array to a QVariantList, converting each element with QJsonValue::toVariant().

See also fromVariantList() and QJsonValue::toVariant().