QMJsonValue Overview

Overview

With respect to JSON, there are several different basic types:

A QMJsonValue is a wrapper that can take on any of the above types. This provides a convient mechanism in C++ to implement the JSON array and object as these data structures can take on any combintation of basic types themselves. That is, in JSON this is perfectly valid:

[true, 4.8, "Hello World"]

In other high level languages, this can be done, but in C++, this is more difficult. To overcome this, the JSON array / object is implemented as an array / hash of QMJsonValues.

[QMJsonValue(true), QMJsonValue(4.8), QMJsonValue("Hello World")]

Unlike other librarys, the QMJsonValue takes this one step further and provides support for complex JSON types, allowing for automatic marshaling and unmarshaling of custom types to and from JSON. For more information about JSON, see the following:

http://www.json.org

Creation

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

auto value = QMPointer<QMJsonValue>(new QMJsonValue(4.8));

The QMJsonValue provides constructors for all of the basic types, as well as some non-JSON specific types to ease the use of this API. For example, without additional constructors, type casting would be required constantly:

auto value = QMPointer<QMJsonValue>(new QMJsonValue((double)4));

This library provides constructor overloads for all of the integer types (8bit s/u to 64bit s/u), as well as floating point and ASCII character pointers.

Getting / Setting

Once a QMJsonValue is created, it's internal type cannot be changed. That is to say, it's not possible to take an existing QMJsonValue that is currently storing a double, and tell it to start storing a string. With some of the basic types, it is however possible to convert from one type to another. For example,

auto value = QMPointer<QMJsonValue>(new QMJsonValue(4.8));
qDebug() << value->toString(); // "4.8"

Ideally, if the internal type is a bool, you would only use the toBool and fromBool functions. Furthermore, automatic type conversions are not provided if the user provides a default value.

auto value = QMPointer<QMJsonValue>(new QMJsonValue(4.8));
qDebug() << value->toString("test"); // "test"

If the QMJsonValue is storing a complex type, the to<T> and from<T> functions must be used, as generics are not provided.

auto value = QMPointer<QMJsonValue>(new QMJsonValue(QSize(4, 8)));
qDebug() << value->to<QSize>(QSize(15, 16)); // QSize(4, 8)
qDebug() << value->from<QSize>(QSize(15, 16)); // true

Testing

There are multiple ways to determine what type a QMJsonValue is if the type is basic.

Genreally speaking, the isNull, isBool, isDouble, isString, isArray, isObject are preferred as they are faster and more concise. If the type you are attempting to identify is a complex type, you need to use the is<xxx> function as this is the only way to identify a complex type.

auto value = QMPointer<QMJsonValue>(new QMJsonValue(QSize(4, 8)));
qDebug() << value->is<QSize>(); // true

To / From JSON

With most JSON, at some point, you will likely need to convert the JSON to and from a JSON string. This can be doen with the QMJsonValue::toJson and QMJsonValue::fromJson functions. This library also provides file operations as well, which allow for JSON to be read / written to / from a file usign QMJsonValue::toJsonFile and QMJsonValue::fromJsonFile

Complex Types

QMJsonValue can store any JSON type natively. There is nothing the user must do for basic support other than provide a couple of support functions for the class if they are not alreayd provided, which include:

If these are not provided, it's like the compiler will complain in an attempt to put the complex type inside the QMJsonValue (as the compiler attempts to create the template type). If the user would like to use QMJsonValue::toJson and QMJsonValue::fromJson, three things must be done. First, the user must create template overloads for QMJsonType::toComplexJson and QMJsonType::fromComplexJson, and then the user must register the QMJsonType::fromComplexJson function using QMJsonValue::registerFromComplexJson.

For a complete exmaple, see main.cpp, qmjsontype_qsize.cpp and qmjsontype_qrect.cpp