QMJsonArray Overview

The QMJsonArray implements a JSON array. Internally, the QMJsonArray is a QList<QMPointer<QMJsonValue> >. Thus, all of the rules that apply to QList with respect to performance are basically the same with QMJsonArray. Like the rest of the QtMark JSON Library, all of the QMJsonValues stored in the QMJsonArray as stored as QMPointers.

Creation

Like all of the classes in this library, QMJsonArray must be created using a managed QMPointer.

auto array = QMPointer<QMJsonArray>(new QMJsonArray());

Array Manipulation

There are a number of ways to add to QMJsonArray:

There is no needed to manually wrap a value in a QMJsonValue, as this will be done for you:

auto array = QMPointer<QMJsonArray>(new QMJsonArray());
array->append(4.8);

There are also a number of functions to remove an item from the QMJsonArray. The major ones are:

Array Information

QMJsonArray::isEmpty will tell you if the QMJsonArray has any QMJsonValues in it. You can also call QMJsonArray::count to get the current number of QMJsonValues in the QMJsonArray.

auto array = QMPointer<QMJsonArray>(new QMJsonArray());
array->append(4.8);
qDebug() << array->isEmpty(); // false
qDebug() << array->count(); // 1

Lookup Functions

There are number of functions that perform their operations by doing a lookup using an existing QMJsonValue.

These functions do a pointer comparison to validate that QMJsonValue being referenced has been found. This means that currently, you cannot create a second QMJsonValue with the same wrapped value, and expect these functions to perform the intended results.

auto array = QMPointer<QMJsonArray>(new QMJsonArray());
auto value = QMPointer<QMJsonValue>(new QMJsonValue(4.8));
array->append(15.16);
array->append(value);
qDebug() << array->contains(value); // true
qDebug() << array->contains(new QMJsonValue(4.8)); // false
qDebug() << array->contains(new QMJsonValue(15.16)); // false

Convenience Functions

Both the QMJsonObject and QMJsonArray classes provide the QMJsonValue unwrap functions for convenience. This applies for:

Allowing for shorthand conversions:

auto array = QMPointer<QMJsonArray>(new QMJsonArray());
array->append(4.8);
qDebug() << array->toDouble(0); // 4.8
qDebug() << array->toString(0); // "4.8"
// Instead of
qDebug() << array->value(0)->toDouble(); // 4.8
qDebug() << array->value(0)->toString(); // "4.8"

To / From JSON

To convert a QMJsonArray to JSON, you must wrap the QMJsonArray in a QMJsonValue and use QMJsonValue::toJson and QMJsonValue::fromJson

auto array = QMPointer<QMJsonArray>(new QMJsonArray());
auto document = QMPointer<QMJsonValue>(new QMJsonValue(array));
array->append(4.8);
qDebug() << document->toJson(QMJsonFormat_Optimized); // "[4.8]"