QMJsonObject Overview

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

Creation

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

auto object = QMPointer<QMJsonObject>(new QMJsonObject());

Object Manipulation

There is basically one way to add items to a QMJsonObject:

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

auto object = QMPointer<QMJsonObject>(new QMJsonObject());
object->insert("key0", 4.8);

There are also a number of functions to remove an item from the QMJsonObject. These are:

Object Information

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

auto object = QMPointer<QMJsonObject>(new QMJsonObject());
object->insert("key0", 4.8);
qDebug() << object->isEmpty(); // false
qDebug() << object->count(); // 1

Convenience Functions

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

Allowing for shorthand conversions:

auto object = QMPointer<QMJsonObject>(new QMJsonObject());
object->insert("key0", 4.8);
qDebug() << object->toDouble("key0"); // 4.8
qDebug() << object->toString("key0"); // "4.8"
// Instead of
qDebug() << object->value("key0")->toDouble(); // 4.8
qDebug() << object->value("key0")->toString(); // "4.8"

To / From JSON

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

auto object = QMPointer<QMJsonObject>(new QMJsonObject());
auto document = QMPointer<QMJsonValue>(new QMJsonValue(object));
object->insert("key0", 4.8);
qDebug() << document->toJson(QMJsonFormat_Optimized); // "{"key0":4.8}"