Qx v0.5.7
Qt Extensions Library
Loading...
Searching...
No Matches
qx-json.h File Reference

The qx-json header file provides various utilities for JSON data manipulation. More...

#include "qx/core/qx_core_export.h"
#include <QString>
#include <QJsonValueRef>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QFile>
#include <QFileInfo>
#include "qx/core/qx-abstracterror.h"
#include "qx/core/qx-error.h"
#include "qx/utility/qx-macros.h"
#include "qx/utility/qx-concepts.h"
Include dependency graph for qx-json.h:

Go to the source code of this file.

Classes

class  QxJson::Array
 The array class represents a JSON array node for use in error contexts. More...
 
class  QxJson::ArrayElement
 The array element key class represents a JSON Array element node for use in error contexts. More...
 
class  QxJson::Document
 The document class represents a JSON document node for use in error contexts. More...
 
class  QxJson::File
 The file class represents a JSON file node for use in error contexts. More...
 
class  Qx::JsonError
 The JsonError class is used to report errors related to JSON manipulation. More...
 
class  QxJson::Object
 The object class represents a JSON object node for use in error contexts. More...
 
class  QxJson::ObjectKey
 The object key class represents a JSON Object key node for use in error contexts. More...
 
class  Qx::QJsonParseErrorAdapter
 Allows QJsonParseError to be used via the Qx::Error interface. More...
 

Namespaces

namespace  Qx
 The Qx namespace is the main namespace through which all non-global functionality of the Qx library is accessed.
 
namespace  QxJson
 The QxJson namespace encapsulates the user-extensible implementation of Qx's JSON parsing facilities.
 

Concepts

concept  QxJson::qjson_type
 
concept  QxJson::json_struct_inside
 
concept  QxJson::json_struct_outside
 
concept  QxJson::json_struct
 
concept  QxJson::json_convertible
 
concept  QxJson::json_override_convertible
 
concept  QxJson::json_keyable
 
concept  QxJson::json_collective
 
concept  QxJson::json_associative
 
concept  QxJson::json_containing
 
concept  QxJson::json_optional
 
concept  Qx::json_root
 Specifies that a type is a valid analogue for a JSON document root element.
 

Macros

#define QX_JSON_MEMBER(member)   QxJsonPrivate::makeMemberMetadata<#member>(&StructT::member)
 
#define QX_JSON_MEMBER_ALIASED(member, key)   QxJsonPrivate::makeMemberMetadata<key>(&StructT::member)
 
#define QX_JSON_MEMBER_OVERRIDE(Struct, member, ...)
 
#define QX_JSON_STRUCT(...)   __QX_JSON_META_STRUCT_INSIDE(std::make_tuple(QX_FOR_EACH_DELIM(QX_JSON_MEMBER, __VA_ARGS__)))
 
#define QX_JSON_STRUCT_OUTSIDE(Struct, ...)   __QX_JSON_META_STRUCT_OUTSIDE(Struct, std::make_tuple(QX_FOR_EACH_DELIM(QX_JSON_MEMBER, __VA_ARGS__)))
 
#define QX_JSON_STRUCT_OUTSIDE_X(Struct, ...)   __QX_JSON_META_STRUCT_OUTSIDE(Struct, std::make_tuple(__VA_ARGS__))
 
#define QX_JSON_STRUCT_X(...)   __QX_JSON_META_STRUCT_INSIDE(std::make_tuple(__VA_ARGS__))
 

Typedefs

using QxJson::ContextNode = std::variant<File, Document, Object, ObjectKey, Array, ArrayElement>
 

Functions

QString Qx::asString (const QJsonValue &value)
 
QList< QJsonValueQx::findAllValues (const QJsonValue &rootValue, QStringView key)
 
template<typename Key , class Value >
Key QxJson::keygen (const Value &value)=delete
 The keygen template function acts as an interface through which the derivation of a key for a given type when used in associative containers is defined.
 
template<typename T >
requires QxJson::json_containing<T>
JsonError Qx::parseJson (T &parsed, const QJsonArray &array)
 
template<typename T >
requires json_root<T>
JsonError Qx::parseJson (T &parsed, const QJsonDocument &doc)
 
template<typename T >
requires QxJson::json_struct<T>
JsonError Qx::parseJson (T &parsed, const QJsonObject &obj)
 
template<typename T >
requires json_root<T>
JsonError Qx::parseJson (T &parsed, const QString &filePath)
 
template<typename T >
requires json_root<T>
JsonError Qx::parseJson (T &parsed, QFile &file)
 

Detailed Description

The mechanisms of this file introduce a highly flexible, simple to use, declarative mechanism for parsing 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());
}
See also
QX_JSON_STRUCT(), and QxJson.

Macro Definition Documentation

◆ QX_JSON_MEMBER

#define QX_JSON_MEMBER ( member)    QxJsonPrivate::makeMemberMetadata<#member>(&StructT::member)

Used to denote a JSON struct member whose name matches its key when using QX_JSON_STRUCT_X().

See also
QX_JSON_STRUCT_X()

◆ QX_JSON_MEMBER_ALIASED

#define QX_JSON_MEMBER_ALIASED ( member,
key )   QxJsonPrivate::makeMemberMetadata<key>(&StructT::member)

Used to denote a JSON struct member with a key that is different from its name when using QX_JSON_STRUCT_X().

key is to be a string literal.

See also
QX_JSON_STRUCT_X()

◆ QX_JSON_MEMBER_OVERRIDE

#define QX_JSON_MEMBER_OVERRIDE ( Struct,
member,
... )
Value:
namespace QxJson \
{ \
template<> \
struct MemberOverrideCoverter<Struct, #member> \
{ \
__VA_ARGS__\
}; \
}
The QxJson namespace encapsulates the user-extensible implementation of Qx's JSON parsing facilities.
Definition qx-json.cpp:346

Used to define a member/key specific value parsing override for a JSON-tried struct. The specified member will be parsed using the provided function instead of a potentially available generic one for that type.

Must be used in namespace scope, outside of the struct definition.

struct MySpecialStruct
{
int number;
QString name;
QX_JSON_STRUCT(number, name);
}
QX_JSON_MEMBER_OVERRIDE(MySpecialStruct, name,
static Qx::JsonError fromJson(QString& member, const QJsonValue& jv)
{
member = "OverridenName";
return Qx::JsonError();
}
)

◆ QX_JSON_STRUCT

#define QX_JSON_STRUCT ( ...)    __QX_JSON_META_STRUCT_INSIDE(std::make_tuple(QX_FOR_EACH_DELIM(QX_JSON_MEMBER, __VA_ARGS__)))

Specifies that a struct is a JSON-tied struct, which enables support for automatic parsing of a corresponding JSON object.

The name of each included member must match the name their corresponding JSON key.

struct MyStruct
{
int number;
QString name;
QX_JSON_STRUCT(number, name);
}
See also
QX_JSON_STRUCT_OUTSIDE() and QX_JSON_STRUCT_X()

◆ QX_JSON_STRUCT_OUTSIDE

#define QX_JSON_STRUCT_OUTSIDE ( Struct,
... )   __QX_JSON_META_STRUCT_OUTSIDE(Struct, std::make_tuple(QX_FOR_EACH_DELIM(QX_JSON_MEMBER, __VA_ARGS__)))

Same as QX_JSON_STRUCT(), but is used outside of a struct instead of inside.

struct MyStruct
{
int number;
QString name;
}
// At global scope
QX_JSON_STRUCT_OUTSIDE(number, name);

This is useful for hiding the JSON parsing implementation details of a public struct within a different source file.

See also
QX_JSON_STRUCT()

◆ QX_JSON_STRUCT_OUTSIDE_X

#define QX_JSON_STRUCT_OUTSIDE_X ( Struct,
... )   __QX_JSON_META_STRUCT_OUTSIDE(Struct, std::make_tuple(__VA_ARGS__))

Same as QX_JSON_STRUCT_X(), but is used outside of a struct instead of inside.

See also
QX_JSON_STRUCT_OUTSIDE()

◆ QX_JSON_STRUCT_X

#define QX_JSON_STRUCT_X ( ...)    __QX_JSON_META_STRUCT_INSIDE(std::make_tuple(__VA_ARGS__))

Same as QX_JSON_STRUCT(), but allows for customization of member keys.

Each member must be wrapped in QX_JSON_MEMBER() or QX_JSON_MEMBER_ALIASED().

struct MyStruct
{
int number;
QString name;
QX_JSON_MEMBER(number),
QX_JSON_MEMBER_ALIASED(name, "aliasName")
);
}