Qx v0.5.7
Qt Extensions Library
|
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"
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< QJsonValue > | Qx::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) |
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:
can easily be parsed into a corresponding set of C++ data structures like so:
#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().
#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.
#define QX_JSON_MEMBER_OVERRIDE | ( | Struct, | |
member, | |||
... ) |
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.
#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.
#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.
This is useful for hiding the JSON parsing implementation details of a public struct within a different source file.
#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.
#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().