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;
};
struct MyJson
{
Info info;
};
int main()
{
QFile jsonFile(
"data.json");
MyJson myJsonDoc;
...
}
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()
{
...
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().