QtCraft API C++ classes QML types Handbook

QcJsonFieldReader Struct

Reads typed fields of one untrusted JSON object. More...

Header: #include <common/content/registry/QcJsonFieldReader.h>

Public Functions

void adopt(const QcJsonFieldReader &nested)
bool boolean(const QString &key, bool fallback)
std::optional<quint32> colour(const QString &key)
void fail(const QString &message)
int integer(const QString &key, int fallback, int low, int high)
QString name(const QString &key, const QString &packNamespace, const QString &fallback = {})
void nestedObject(const QString &key, Read read)
double number(const QString &key, double fallback, double low, double high)
bool ok() const
QString string(const QString &key, const QString &fallback = {})

Public Variables

QString context
QString error
const QJsonObject &json

Detailed Description

The first problem is kept as error, prefixed with context; later problems do not replace it. A read that fails returns its fallback; reads after a failure still return the values they find.

Member Function Documentation

void QcJsonFieldReader::adopt(const QcJsonFieldReader &nested)

Takes over the error of the reader nested for a nested object, if it has one and this reader has none yet. The nested reader's context names the place in full (it starts with this reader's context), so its error is kept as it is, without a second prefix.

bool QcJsonFieldReader::boolean(const QString &key, bool fallback)

Returns the boolean at key; fallback when the key is missing or not a boolean (the latter fails).

std::optional<quint32> QcJsonFieldReader::colour(const QString &key)

Returns the colour at key written as "#rrggbb", as 0xRRGGBB; none when the key is missing or empty, or when the text is no such colour (which fails).

void QcJsonFieldReader::fail(const QString &message)

Records message, prefixed with the context, as the error unless there already is one.

int QcJsonFieldReader::integer(const QString &key, int fallback, int low, int high)

Returns the whole number at key; fallback when the key is missing or not a whole number from low to high (the latter fails).

QString QcJsonFieldReader::name(const QString &key, const QString &packNamespace, const QString &fallback = {})

Returns the content name at key (fallback when missing); unqualified names get packNamespace. An invalid name fails and gives an empty string.

template <typename Read> void QcJsonFieldReader::nestedObject(const QString &key, Read read)

Reads the optional object at key with a nested reader (context "<context> <key>") and adopts its error. read is called as read(const QJsonObject &object, QcJsonFieldReader &nested).

double QcJsonFieldReader::number(const QString &key, double fallback, double low, double high)

Returns the number at key; fallback when the key is missing or not a number from low to high (the latter fails).

bool QcJsonFieldReader::ok() const

Returns whether no read has failed yet.

QString QcJsonFieldReader::string(const QString &key, const QString &fallback = {})

Returns the string at key; fallback when the key is missing or not a string (the latter fails).

Member Variable Documentation

QString QcJsonFieldReader::context

Where the object comes from, e.g. "advancement 'qtcraft:root'"; prefixes the error.

QString QcJsonFieldReader::error

The first problem, prefixed with the context; empty while all reads succeeded.

const QJsonObject &QcJsonFieldReader::json

The object read; the reader does not own it.