Qx v0.6
Qt Extensions Library
Loading...
Searching...
No Matches
Qx Declarative JSON

Qx features a highly flexible, simple to use, declarative mechanism for parsing/serializing JSON data into user structs and other types.

For example, the following JSON data:

{
"title": "Sample JSON Data",
"info": {
"rating": 10,
"cool": true
},
"reviews": [
"Wicked!",
"Awesome!",
"Fantastic!"
]
}

can easily be parsed into a corresponding set of C++ data structures like so:

struct Info
{
int rating;
bool cool;
QX_JSON_STRUCT(rating, cool);
};
struct MyJson
{
QString title;
Info info;
QList<QString> reviews;
QX_JSON_STRUCT(title, info, reviews);
};
int main()
{
QFile jsonFile("data.json");
MyJson myJsonDoc;
// Parse into custom structures
Qx::JsonError je = Qx::parseJson(myJsonDoc, jsonFile);
Q_ASSERT(!je.isValid());
...
}
The JsonError class is used to report errors related to JSON manipulation.
Definition qx-json.h:156
bool isValid() const
Definition qx-json.cpp:189
JsonError parseJson(T &parsed, const QJsonObject &obj)
Definition qx-json.h:729
The qx-json header file offers various utilities for JSON data manipulation.
#define QX_JSON_STRUCT(...)
Definition qx-json.h:58

Likewise, the structure can be serialized back out into textual JSON data with:

int main()
{
...
// Serialize to JSON
je = Qx::serializeJson(jsonFile, myJsonDoc);
Q_ASSERT(!je.isValid());
}
void serializeJson(QJsonObject &serialized, const T &struc)
Definition qx-json.h:739

This system is accessed through the qx-json.h header, predominantly with QX_JSON_STRUCT().