NOISSUE sanitize Json
Removes magical parameter madness. All require* can throw All ensure* need a default value and never throw
This commit is contained in:
parent
dde35a0eb8
commit
24db645167
@ -44,7 +44,7 @@ static bool isBinaryJson(const QByteArray &data)
|
|||||||
decltype(QJsonDocument::BinaryFormatTag) tag = QJsonDocument::BinaryFormatTag;
|
decltype(QJsonDocument::BinaryFormatTag) tag = QJsonDocument::BinaryFormatTag;
|
||||||
return memcmp(data.constData(), &tag, sizeof(QJsonDocument::BinaryFormatTag)) == 0;
|
return memcmp(data.constData(), &tag, sizeof(QJsonDocument::BinaryFormatTag)) == 0;
|
||||||
}
|
}
|
||||||
QJsonDocument ensureDocument(const QByteArray &data, const QString &what)
|
QJsonDocument requireDocument(const QByteArray &data, const QString &what)
|
||||||
{
|
{
|
||||||
if (isBinaryJson(data))
|
if (isBinaryJson(data))
|
||||||
{
|
{
|
||||||
@ -66,11 +66,11 @@ QJsonDocument ensureDocument(const QByteArray &data, const QString &what)
|
|||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QJsonDocument ensureDocument(const QString &filename, const QString &what)
|
QJsonDocument requireDocument(const QString &filename, const QString &what)
|
||||||
{
|
{
|
||||||
return ensureDocument(FS::read(filename), what);
|
return requireDocument(FS::read(filename), what);
|
||||||
}
|
}
|
||||||
QJsonObject ensureObject(const QJsonDocument &doc, const QString &what)
|
QJsonObject requireObject(const QJsonDocument &doc, const QString &what)
|
||||||
{
|
{
|
||||||
if (!doc.isObject())
|
if (!doc.isObject())
|
||||||
{
|
{
|
||||||
@ -78,7 +78,7 @@ QJsonObject ensureObject(const QJsonDocument &doc, const QString &what)
|
|||||||
}
|
}
|
||||||
return doc.object();
|
return doc.object();
|
||||||
}
|
}
|
||||||
QJsonArray ensureArray(const QJsonDocument &doc, const QString &what)
|
QJsonArray requireArray(const QJsonDocument &doc, const QString &what)
|
||||||
{
|
{
|
||||||
if (!doc.isArray())
|
if (!doc.isArray())
|
||||||
{
|
{
|
||||||
@ -140,10 +140,9 @@ QJsonValue toJson<QVariant>(const QVariant &variant)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<> QByteArray ensureIsType<QByteArray>(const QJsonValue &value, const Requirement,
|
template<> QByteArray requireIsType<QByteArray>(const QJsonValue &value, const QString &what)
|
||||||
const QString &what)
|
|
||||||
{
|
{
|
||||||
const QString string = ensureIsType<QString>(value, Required, what);
|
const QString string = ensureIsType<QString>(value, what);
|
||||||
// ensure that the string can be safely cast to Latin1
|
// ensure that the string can be safely cast to Latin1
|
||||||
if (string != QString::fromLatin1(string.toLatin1()))
|
if (string != QString::fromLatin1(string.toLatin1()))
|
||||||
{
|
{
|
||||||
@ -152,7 +151,7 @@ template<> QByteArray ensureIsType<QByteArray>(const QJsonValue &value, const Re
|
|||||||
return QByteArray::fromHex(string.toLatin1());
|
return QByteArray::fromHex(string.toLatin1());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QJsonArray ensureIsType<QJsonArray>(const QJsonValue &value, const Requirement, const QString &what)
|
template<> QJsonArray requireIsType<QJsonArray>(const QJsonValue &value, const QString &what)
|
||||||
{
|
{
|
||||||
if (!value.isArray())
|
if (!value.isArray())
|
||||||
{
|
{
|
||||||
@ -162,7 +161,7 @@ template<> QJsonArray ensureIsType<QJsonArray>(const QJsonValue &value, const Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<> QString ensureIsType<QString>(const QJsonValue &value, const Requirement, const QString &what)
|
template<> QString requireIsType<QString>(const QJsonValue &value, const QString &what)
|
||||||
{
|
{
|
||||||
if (!value.isString())
|
if (!value.isString())
|
||||||
{
|
{
|
||||||
@ -171,8 +170,7 @@ template<> QString ensureIsType<QString>(const QJsonValue &value, const Requirem
|
|||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> bool ensureIsType<bool>(const QJsonValue &value, const Requirement,
|
template<> bool requireIsType<bool>(const QJsonValue &value, const QString &what)
|
||||||
const QString &what)
|
|
||||||
{
|
{
|
||||||
if (!value.isBool())
|
if (!value.isBool())
|
||||||
{
|
{
|
||||||
@ -181,8 +179,7 @@ template<> bool ensureIsType<bool>(const QJsonValue &value, const Requirement,
|
|||||||
return value.toBool();
|
return value.toBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> double ensureIsType<double>(const QJsonValue &value, const Requirement,
|
template<> double requireIsType<double>(const QJsonValue &value, const QString &what)
|
||||||
const QString &what)
|
|
||||||
{
|
{
|
||||||
if (!value.isDouble())
|
if (!value.isDouble())
|
||||||
{
|
{
|
||||||
@ -191,10 +188,9 @@ template<> double ensureIsType<double>(const QJsonValue &value, const Requiremen
|
|||||||
return value.toDouble();
|
return value.toDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> int ensureIsType<int>(const QJsonValue &value, const Requirement,
|
template<> int requireIsType<int>(const QJsonValue &value, const QString &what)
|
||||||
const QString &what)
|
|
||||||
{
|
{
|
||||||
const double doubl = ensureIsType<double>(value, Required, what);
|
const double doubl = requireIsType<double>(value, what);
|
||||||
if (fmod(doubl, 1) != 0)
|
if (fmod(doubl, 1) != 0)
|
||||||
{
|
{
|
||||||
throw JsonException(what + " is not an integer");
|
throw JsonException(what + " is not an integer");
|
||||||
@ -202,10 +198,9 @@ template<> int ensureIsType<int>(const QJsonValue &value, const Requirement,
|
|||||||
return int(doubl);
|
return int(doubl);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QDateTime ensureIsType<QDateTime>(const QJsonValue &value, const Requirement,
|
template<> QDateTime requireIsType<QDateTime>(const QJsonValue &value, const QString &what)
|
||||||
const QString &what)
|
|
||||||
{
|
{
|
||||||
const QString string = ensureIsType<QString>(value, Required, what);
|
const QString string = requireIsType<QString>(value, what);
|
||||||
const QDateTime datetime = QDateTime::fromString(string, Qt::ISODate);
|
const QDateTime datetime = QDateTime::fromString(string, Qt::ISODate);
|
||||||
if (!datetime.isValid())
|
if (!datetime.isValid())
|
||||||
{
|
{
|
||||||
@ -214,10 +209,9 @@ template<> QDateTime ensureIsType<QDateTime>(const QJsonValue &value, const Requ
|
|||||||
return datetime;
|
return datetime;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QUrl ensureIsType<QUrl>(const QJsonValue &value, const Requirement,
|
template<> QUrl requireIsType<QUrl>(const QJsonValue &value, const QString &what)
|
||||||
const QString &what)
|
|
||||||
{
|
{
|
||||||
const QString string = ensureIsType<QString>(value, Required, what);
|
const QString string = ensureIsType<QString>(value, what);
|
||||||
if (string.isEmpty())
|
if (string.isEmpty())
|
||||||
{
|
{
|
||||||
return QUrl();
|
return QUrl();
|
||||||
@ -230,15 +224,16 @@ template<> QUrl ensureIsType<QUrl>(const QJsonValue &value, const Requirement,
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QDir ensureIsType<QDir>(const QJsonValue &value, const Requirement, const QString &what)
|
template<> QDir requireIsType<QDir>(const QJsonValue &value, const QString &what)
|
||||||
{
|
{
|
||||||
const QString string = ensureIsType<QString>(value, Required, what);
|
const QString string = requireIsType<QString>(value, what);
|
||||||
|
// FIXME: does not handle invalid characters!
|
||||||
return QDir::current().absoluteFilePath(string);
|
return QDir::current().absoluteFilePath(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QUuid ensureIsType<QUuid>(const QJsonValue &value, const Requirement, const QString &what)
|
template<> QUuid requireIsType<QUuid>(const QJsonValue &value, const QString &what)
|
||||||
{
|
{
|
||||||
const QString string = ensureIsType<QString>(value, Required, what);
|
const QString string = requireIsType<QString>(value, what);
|
||||||
const QUuid uuid = QUuid(string);
|
const QUuid uuid = QUuid(string);
|
||||||
if (uuid.toString() != string) // converts back => valid
|
if (uuid.toString() != string) // converts back => valid
|
||||||
{
|
{
|
||||||
@ -247,7 +242,7 @@ template<> QUuid ensureIsType<QUuid>(const QJsonValue &value, const Requirement,
|
|||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QJsonObject ensureIsType<QJsonObject>(const QJsonValue &value, const Requirement, const QString &what)
|
template<> QJsonObject requireIsType<QJsonObject>(const QJsonValue &value, const QString &what)
|
||||||
{
|
{
|
||||||
if (!value.isObject())
|
if (!value.isObject())
|
||||||
{
|
{
|
||||||
@ -256,7 +251,7 @@ template<> QJsonObject ensureIsType<QJsonObject>(const QJsonValue &value, const
|
|||||||
return value.toObject();
|
return value.toObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QVariant ensureIsType<QVariant>(const QJsonValue &value, const Requirement, const QString &what)
|
template<> QVariant requireIsType<QVariant>(const QJsonValue &value, const QString &what)
|
||||||
{
|
{
|
||||||
if (value.isNull() || value.isUndefined())
|
if (value.isNull() || value.isUndefined())
|
||||||
{
|
{
|
||||||
@ -265,7 +260,7 @@ template<> QVariant ensureIsType<QVariant>(const QJsonValue &value, const Requir
|
|||||||
return value.toVariant();
|
return value.toVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> QJsonValue ensureIsType<QJsonValue>(const QJsonValue &value, const Requirement, const QString &what)
|
template<> QJsonValue requireIsType<QJsonValue>(const QJsonValue &value, const QString &what)
|
||||||
{
|
{
|
||||||
if (value.isNull() || value.isUndefined())
|
if (value.isNull() || value.isUndefined())
|
||||||
{
|
{
|
||||||
|
117
logic/Json.h
117
logic/Json.h
@ -18,11 +18,6 @@ namespace Json
|
|||||||
{
|
{
|
||||||
DECLARE_EXCEPTION(Json);
|
DECLARE_EXCEPTION(Json);
|
||||||
|
|
||||||
enum Requirement
|
|
||||||
{
|
|
||||||
Required
|
|
||||||
};
|
|
||||||
|
|
||||||
/// @throw FileSystemException
|
/// @throw FileSystemException
|
||||||
void write(const QJsonDocument &doc, const QString &filename);
|
void write(const QJsonDocument &doc, const QString &filename);
|
||||||
/// @throw FileSystemException
|
/// @throw FileSystemException
|
||||||
@ -36,13 +31,13 @@ QByteArray toText(const QJsonObject &obj);
|
|||||||
QByteArray toText(const QJsonArray &array);
|
QByteArray toText(const QJsonArray &array);
|
||||||
|
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
QJsonDocument ensureDocument(const QByteArray &data, const QString &what = "Document");
|
QJsonDocument requireDocument(const QByteArray &data, const QString &what = "Document");
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
QJsonDocument ensureDocument(const QString &filename, const QString &what = "Document");
|
QJsonDocument requireDocument(const QString &filename, const QString &what = "Document");
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
QJsonObject ensureObject(const QJsonDocument &doc, const QString &what = "Document");
|
QJsonObject requireObject(const QJsonDocument &doc, const QString &what = "Document");
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
QJsonArray ensureArray(const QJsonDocument &doc, const QString &what = "Document");
|
QJsonArray requireArray(const QJsonDocument &doc, const QString &what = "Document");
|
||||||
|
|
||||||
/////////////////// WRITING ////////////////////
|
/////////////////// WRITING ////////////////////
|
||||||
|
|
||||||
@ -107,22 +102,36 @@ QJsonArray toJsonArray(const QList<T> &container)
|
|||||||
|
|
||||||
////////////////// READING ////////////////////
|
////////////////// READING ////////////////////
|
||||||
|
|
||||||
|
/// @throw JsonException
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T ensureIsType(const QJsonValue &value, const Requirement requirement = Required, const QString &what = "Value");
|
T requireIsType(const QJsonValue &value, const QString &what = "Value");
|
||||||
|
|
||||||
template<> double ensureIsType<double>(const QJsonValue &value, const Requirement, const QString &what);
|
/// @throw JsonException
|
||||||
template<> bool ensureIsType<bool>(const QJsonValue &value, const Requirement, const QString &what);
|
template<> double requireIsType<double>(const QJsonValue &value, const QString &what);
|
||||||
template<> int ensureIsType<int>(const QJsonValue &value, const Requirement, const QString &what);
|
/// @throw JsonException
|
||||||
template<> QJsonObject ensureIsType<QJsonObject>(const QJsonValue &value, const Requirement, const QString &what);
|
template<> bool requireIsType<bool>(const QJsonValue &value, const QString &what);
|
||||||
template<> QJsonArray ensureIsType<QJsonArray>(const QJsonValue &value, const Requirement, const QString &what);
|
/// @throw JsonException
|
||||||
template<> QJsonValue ensureIsType<QJsonValue>(const QJsonValue &value, const Requirement, const QString &what);
|
template<> int requireIsType<int>(const QJsonValue &value, const QString &what);
|
||||||
template<> QByteArray ensureIsType<QByteArray>(const QJsonValue &value, const Requirement, const QString &what);
|
/// @throw JsonException
|
||||||
template<> QDateTime ensureIsType<QDateTime>(const QJsonValue &value, const Requirement, const QString &what);
|
template<> QJsonObject requireIsType<QJsonObject>(const QJsonValue &value, const QString &what);
|
||||||
template<> QVariant ensureIsType<QVariant>(const QJsonValue &value, const Requirement, const QString &what);
|
/// @throw JsonException
|
||||||
template<> QString ensureIsType<QString>(const QJsonValue &value, const Requirement, const QString &what);
|
template<> QJsonArray requireIsType<QJsonArray>(const QJsonValue &value, const QString &what);
|
||||||
template<> QUuid ensureIsType<QUuid>(const QJsonValue &value, const Requirement, const QString &what);
|
/// @throw JsonException
|
||||||
template<> QDir ensureIsType<QDir>(const QJsonValue &value, const Requirement, const QString &what);
|
template<> QJsonValue requireIsType<QJsonValue>(const QJsonValue &value, const QString &what);
|
||||||
template<> QUrl ensureIsType<QUrl>(const QJsonValue &value, const Requirement, const QString &what);
|
/// @throw JsonException
|
||||||
|
template<> QByteArray requireIsType<QByteArray>(const QJsonValue &value, const QString &what);
|
||||||
|
/// @throw JsonException
|
||||||
|
template<> QDateTime requireIsType<QDateTime>(const QJsonValue &value, const QString &what);
|
||||||
|
/// @throw JsonException
|
||||||
|
template<> QVariant requireIsType<QVariant>(const QJsonValue &value, const QString &what);
|
||||||
|
/// @throw JsonException
|
||||||
|
template<> QString requireIsType<QString>(const QJsonValue &value, const QString &what);
|
||||||
|
/// @throw JsonException
|
||||||
|
template<> QUuid requireIsType<QUuid>(const QJsonValue &value, const QString &what);
|
||||||
|
/// @throw JsonException
|
||||||
|
template<> QDir requireIsType<QDir>(const QJsonValue &value, const QString &what);
|
||||||
|
/// @throw JsonException
|
||||||
|
template<> QUrl requireIsType<QUrl>(const QJsonValue &value, const QString &what);
|
||||||
|
|
||||||
// the following functions are higher level functions, that make use of the above functions for
|
// the following functions are higher level functions, that make use of the above functions for
|
||||||
// type conversion
|
// type conversion
|
||||||
@ -133,26 +142,30 @@ T ensureIsType(const QJsonValue &value, const T default_, const QString &what =
|
|||||||
{
|
{
|
||||||
return default_;
|
return default_;
|
||||||
}
|
}
|
||||||
return ensureIsType<T>(value, Required, what);
|
try
|
||||||
|
{
|
||||||
|
return requireIsType<T>(value, what);
|
||||||
|
}
|
||||||
|
catch (JsonException &)
|
||||||
|
{
|
||||||
|
return default_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T ensureIsType(const QJsonObject &parent, const QString &key,
|
T requireIsType(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__")
|
||||||
const Requirement requirement = Required,
|
|
||||||
const QString &what = "__placeholder__")
|
|
||||||
{
|
{
|
||||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
||||||
if (!parent.contains(key))
|
if (!parent.contains(key))
|
||||||
{
|
{
|
||||||
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
||||||
}
|
}
|
||||||
return ensureIsType<T>(parent.value(key), requirement, localWhat);
|
return requireIsType<T>(parent.value(key), localWhat);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T ensureIsType(const QJsonObject &parent, const QString &key, const T default_,
|
T ensureIsType(const QJsonObject &parent, const QString &key, const T default_, const QString &what = "__placeholder__")
|
||||||
const QString &what = "__placeholder__")
|
|
||||||
{
|
{
|
||||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
||||||
if (!parent.contains(key))
|
if (!parent.contains(key))
|
||||||
@ -163,53 +176,49 @@ T ensureIsType(const QJsonObject &parent, const QString &key, const T default_,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
QList<T> ensureIsArrayOf(const QJsonDocument &doc)
|
QList<T> requireIsArrayOf(const QJsonDocument &doc)
|
||||||
{
|
{
|
||||||
const QJsonArray array = ensureArray(doc);
|
const QJsonArray array = requireArray(doc);
|
||||||
QList<T> out;
|
QList<T> out;
|
||||||
for (const QJsonValue val : array)
|
for (const QJsonValue val : array)
|
||||||
{
|
{
|
||||||
out.append(ensureIsType<T>(val, Required, "Document"));
|
out.append(requireIsType<T>(val, "Document"));
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
QList<T> ensureIsArrayOf(const QJsonValue &value, const Requirement = Required,
|
QList<T> ensureIsArrayOf(const QJsonValue &value, const QString &what = "Value")
|
||||||
const QString &what = "Value")
|
|
||||||
{
|
{
|
||||||
const QJsonArray array = ensureIsType<QJsonArray>(value, Required, what);
|
const QJsonArray array = requireIsType<QJsonArray>(value, what);
|
||||||
QList<T> out;
|
QList<T> out;
|
||||||
for (const QJsonValue val : array)
|
for (const QJsonValue val : array)
|
||||||
{
|
{
|
||||||
out.append(ensureIsType<T>(val, Required, what));
|
out.append(ensureIsType<T>(val, what));
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
QList<T> ensureIsArrayOf(const QJsonValue &value, const QList<T> default_,
|
QList<T> ensureIsArrayOf(const QJsonValue &value, const QList<T> default_, const QString &what = "Value")
|
||||||
const QString &what = "Value")
|
|
||||||
{
|
{
|
||||||
if (value.isUndefined())
|
if (value.isUndefined())
|
||||||
{
|
{
|
||||||
return default_;
|
return default_;
|
||||||
}
|
}
|
||||||
return ensureIsArrayOf<T>(value, Required, what);
|
return ensureIsArrayOf<T>(value, what);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
template <typename T>
|
template <typename T>
|
||||||
QList<T> ensureIsArrayOf(const QJsonObject &parent, const QString &key,
|
QList<T> requireIsArrayOf(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__")
|
||||||
const Requirement requirement = Required,
|
|
||||||
const QString &what = "__placeholder__")
|
|
||||||
{
|
{
|
||||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
||||||
if (!parent.contains(key))
|
if (!parent.contains(key))
|
||||||
{
|
{
|
||||||
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
||||||
}
|
}
|
||||||
return ensureIsArrayOf<T>(parent.value(key), requirement, localWhat);
|
return requireIsArrayOf<T>(parent.value(key), localWhat);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -226,14 +235,22 @@ QList<T> ensureIsArrayOf(const QJsonObject &parent, const QString &key,
|
|||||||
|
|
||||||
// this macro part could be replaced by variadic functions that just pass on their arguments, but that wouldn't work well with IDE helpers
|
// this macro part could be replaced by variadic functions that just pass on their arguments, but that wouldn't work well with IDE helpers
|
||||||
#define JSON_HELPERFUNCTIONS(NAME, TYPE) \
|
#define JSON_HELPERFUNCTIONS(NAME, TYPE) \
|
||||||
inline TYPE ensure##NAME(const QJsonValue &value, const Requirement requirement = Required, const QString &what = "Value") \
|
inline TYPE require##NAME(const QJsonValue &value, const QString &what = "Value") \
|
||||||
{ return ensureIsType<TYPE>(value, requirement, what); } \
|
{ \
|
||||||
|
return requireIsType<TYPE>(value, what); \
|
||||||
|
} \
|
||||||
inline TYPE ensure##NAME(const QJsonValue &value, const TYPE default_, const QString &what = "Value") \
|
inline TYPE ensure##NAME(const QJsonValue &value, const TYPE default_, const QString &what = "Value") \
|
||||||
{ return ensureIsType<TYPE>(value, default_, what); } \
|
{ \
|
||||||
inline TYPE ensure##NAME(const QJsonObject &parent, const QString &key, const Requirement requirement = Required, const QString &what = "__placeholder__") \
|
return ensureIsType<TYPE>(value, default_, what); \
|
||||||
{ return ensureIsType<TYPE>(parent, key, requirement, what); } \
|
} \
|
||||||
|
inline TYPE require##NAME(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__") \
|
||||||
|
{ \
|
||||||
|
return requireIsType<TYPE>(parent, key, what); \
|
||||||
|
} \
|
||||||
inline TYPE ensure##NAME(const QJsonObject &parent, const QString &key, const TYPE default_, const QString &what = "__placeholder") \
|
inline TYPE ensure##NAME(const QJsonObject &parent, const QString &key, const TYPE default_, const QString &what = "__placeholder") \
|
||||||
{ return ensureIsType<TYPE>(parent, key, default_, what); }
|
{ \
|
||||||
|
return ensureIsType<TYPE>(parent, key, default_, what); \
|
||||||
|
}
|
||||||
|
|
||||||
JSON_HELPERFUNCTIONS(Array, QJsonArray)
|
JSON_HELPERFUNCTIONS(Array, QJsonArray)
|
||||||
JSON_HELPERFUNCTIONS(Object, QJsonObject)
|
JSON_HELPERFUNCTIONS(Object, QJsonObject)
|
||||||
|
@ -157,11 +157,11 @@ void MinecraftVersionList::loadBuiltinList()
|
|||||||
qDebug() << "Loading builtin version list.";
|
qDebug() << "Loading builtin version list.";
|
||||||
// grab the version list data from internal resources.
|
// grab the version list data from internal resources.
|
||||||
const QJsonDocument doc =
|
const QJsonDocument doc =
|
||||||
Json::ensureDocument(QString(":/versions/minecraft.json"), "builtin version list");
|
Json::requireDocument(QString(":/versions/minecraft.json"), "builtin version list");
|
||||||
const QJsonObject root = doc.object();
|
const QJsonObject root = doc.object();
|
||||||
|
|
||||||
// parse all the versions
|
// parse all the versions
|
||||||
for (const auto version : Json::ensureArray(root.value("versions")))
|
for (const auto version : Json::requireArray(root.value("versions")))
|
||||||
{
|
{
|
||||||
QJsonObject versionObj = version.toObject();
|
QJsonObject versionObj = version.toObject();
|
||||||
QString versionID = versionObj.value("id").toString("");
|
QString versionID = versionObj.value("id").toString("");
|
||||||
@ -203,9 +203,9 @@ void MinecraftVersionList::loadBuiltinList()
|
|||||||
mcVersion->m_processArguments = versionObj.value("processArguments").toString("legacy");
|
mcVersion->m_processArguments = versionObj.value("processArguments").toString("legacy");
|
||||||
if (versionObj.contains("+traits"))
|
if (versionObj.contains("+traits"))
|
||||||
{
|
{
|
||||||
for (auto traitVal : Json::ensureArray(versionObj.value("+traits")))
|
for (auto traitVal : Json::requireArray(versionObj.value("+traits")))
|
||||||
{
|
{
|
||||||
mcVersion->m_traits.insert(Json::ensureString(traitVal));
|
mcVersion->m_traits.insert(Json::requireString(traitVal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_lookup[versionID] = mcVersion;
|
m_lookup[versionID] = mcVersion;
|
||||||
@ -226,9 +226,9 @@ void MinecraftVersionList::loadMojangList(QJsonDocument jsonDoc, VersionSource s
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
QJsonObject latest = Json::ensureObject(root.value("latest"));
|
QJsonObject latest = Json::requireObject(root.value("latest"));
|
||||||
m_latestReleaseID = Json::ensureString(latest.value("release"));
|
m_latestReleaseID = Json::requireString(latest.value("release"));
|
||||||
m_latestSnapshotID = Json::ensureString(latest.value("snapshot"));
|
m_latestSnapshotID = Json::requireString(latest.value("snapshot"));
|
||||||
}
|
}
|
||||||
catch (Exception &err)
|
catch (Exception &err)
|
||||||
{
|
{
|
||||||
|
@ -74,18 +74,18 @@ bool readOverrideOrders(QString path, PatchOrder &order)
|
|||||||
// and then read it and process it if all above is true.
|
// and then read it and process it if all above is true.
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto obj = Json::ensureObject(doc);
|
auto obj = Json::requireObject(doc);
|
||||||
// check order file version.
|
// check order file version.
|
||||||
auto version = Json::ensureInteger(obj.value("version"));
|
auto version = Json::requireInteger(obj.value("version"));
|
||||||
if (version != currentOrderFileVersion)
|
if (version != currentOrderFileVersion)
|
||||||
{
|
{
|
||||||
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
|
throw JSONValidationError(QObject::tr("Invalid order file version, expected %1")
|
||||||
.arg(currentOrderFileVersion));
|
.arg(currentOrderFileVersion));
|
||||||
}
|
}
|
||||||
auto orderArray = Json::ensureArray(obj.value("order"));
|
auto orderArray = Json::requireArray(obj.value("order"));
|
||||||
for(auto item: orderArray)
|
for(auto item: orderArray)
|
||||||
{
|
{
|
||||||
order.append(Json::ensureString(item));
|
order.append(Json::requireString(item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (JSONValidationError &err)
|
catch (JSONValidationError &err)
|
||||||
|
@ -39,15 +39,15 @@ RawLibraryPtr RawLibrary::fromJson(const QJsonObject &libObj, const QString &fil
|
|||||||
if (libObj.contains("extract"))
|
if (libObj.contains("extract"))
|
||||||
{
|
{
|
||||||
out->applyExcludes = true;
|
out->applyExcludes = true;
|
||||||
auto extractObj = ensureObject(libObj.value("extract"));
|
auto extractObj = requireObject(libObj.value("extract"));
|
||||||
for (auto excludeVal : ensureArray(extractObj.value("exclude")))
|
for (auto excludeVal : requireArray(extractObj.value("exclude")))
|
||||||
{
|
{
|
||||||
out->extract_excludes.append(ensureString(excludeVal));
|
out->extract_excludes.append(requireString(excludeVal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (libObj.contains("natives"))
|
if (libObj.contains("natives"))
|
||||||
{
|
{
|
||||||
QJsonObject nativesObj = ensureObject(libObj.value("natives"));
|
QJsonObject nativesObj = requireObject(libObj.value("natives"));
|
||||||
for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
|
for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
|
||||||
{
|
{
|
||||||
if (!it.value().isString())
|
if (!it.value().isString())
|
||||||
@ -127,7 +127,7 @@ RawLibraryPtr RawLibrary::fromJsonPlus(const QJsonObject &libObj, const QString
|
|||||||
}
|
}
|
||||||
if (libObj.contains("MMC-depend"))
|
if (libObj.contains("MMC-depend"))
|
||||||
{
|
{
|
||||||
const QString dependString = ensureString(libObj.value("MMC-depend"));
|
const QString dependString = requireString(libObj.value("MMC-depend"));
|
||||||
if (dependString == "hard")
|
if (dependString == "hard")
|
||||||
{
|
{
|
||||||
lib->dependType = RawLibrary::Hard;
|
lib->dependType = RawLibrary::Hard;
|
||||||
|
@ -52,7 +52,7 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
|||||||
{
|
{
|
||||||
if (root.contains("order"))
|
if (root.contains("order"))
|
||||||
{
|
{
|
||||||
out->order = ensureInteger(root.value("order"));
|
out->order = requireInteger(root.value("order"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -71,7 +71,7 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
|||||||
{
|
{
|
||||||
if (root.contains(key))
|
if (root.contains(key))
|
||||||
{
|
{
|
||||||
variable = ensureString(root.value(key));
|
variable = requireString(root.value(key));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
|||||||
{
|
{
|
||||||
if (root.contains(key))
|
if (root.contains(key))
|
||||||
{
|
{
|
||||||
return ensureString(root.value(key));
|
return requireString(root.value(key));
|
||||||
}
|
}
|
||||||
return QString();
|
return QString();
|
||||||
};
|
};
|
||||||
@ -101,48 +101,48 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
|||||||
|
|
||||||
if (root.contains("minimumLauncherVersion"))
|
if (root.contains("minimumLauncherVersion"))
|
||||||
{
|
{
|
||||||
out->minimumLauncherVersion = ensureInteger(root.value("minimumLauncherVersion"));
|
out->minimumLauncherVersion = requireInteger(root.value("minimumLauncherVersion"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("tweakers"))
|
if (root.contains("tweakers"))
|
||||||
{
|
{
|
||||||
out->shouldOverwriteTweakers = true;
|
out->shouldOverwriteTweakers = true;
|
||||||
for (auto tweakerVal : ensureArray(root.value("tweakers")))
|
for (auto tweakerVal : requireArray(root.value("tweakers")))
|
||||||
{
|
{
|
||||||
out->overwriteTweakers.append(ensureString(tweakerVal));
|
out->overwriteTweakers.append(requireString(tweakerVal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("+tweakers"))
|
if (root.contains("+tweakers"))
|
||||||
{
|
{
|
||||||
for (auto tweakerVal : ensureArray(root.value("+tweakers")))
|
for (auto tweakerVal : requireArray(root.value("+tweakers")))
|
||||||
{
|
{
|
||||||
out->addTweakers.append(ensureString(tweakerVal));
|
out->addTweakers.append(requireString(tweakerVal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("-tweakers"))
|
if (root.contains("-tweakers"))
|
||||||
{
|
{
|
||||||
for (auto tweakerVal : ensureArray(root.value("-tweakers")))
|
for (auto tweakerVal : requireArray(root.value("-tweakers")))
|
||||||
{
|
{
|
||||||
out->removeTweakers.append(ensureString(tweakerVal));
|
out->removeTweakers.append(requireString(tweakerVal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("+traits"))
|
if (root.contains("+traits"))
|
||||||
{
|
{
|
||||||
for (auto tweakerVal : ensureArray(root.value("+traits")))
|
for (auto tweakerVal : requireArray(root.value("+traits")))
|
||||||
{
|
{
|
||||||
out->traits.insert(ensureString(tweakerVal));
|
out->traits.insert(requireString(tweakerVal));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (root.contains("libraries"))
|
if (root.contains("libraries"))
|
||||||
{
|
{
|
||||||
out->shouldOverwriteLibs = true;
|
out->shouldOverwriteLibs = true;
|
||||||
for (auto libVal : ensureArray(root.value("libraries")))
|
for (auto libVal : requireArray(root.value("libraries")))
|
||||||
{
|
{
|
||||||
auto libObj = ensureObject(libVal);
|
auto libObj = requireObject(libVal);
|
||||||
|
|
||||||
auto lib = RawLibrary::fromJson(libObj, filename);
|
auto lib = RawLibrary::fromJson(libObj, filename);
|
||||||
out->overwriteLibs.append(lib);
|
out->overwriteLibs.append(lib);
|
||||||
@ -151,9 +151,9 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
|||||||
|
|
||||||
if (root.contains("+jarMods"))
|
if (root.contains("+jarMods"))
|
||||||
{
|
{
|
||||||
for (auto libVal : ensureArray(root.value("+jarMods")))
|
for (auto libVal : requireArray(root.value("+jarMods")))
|
||||||
{
|
{
|
||||||
QJsonObject libObj = ensureObject(libVal);
|
QJsonObject libObj = requireObject(libVal);
|
||||||
// parse the jarmod
|
// parse the jarmod
|
||||||
auto lib = Jarmod::fromJson(libObj, filename, out->name);
|
auto lib = Jarmod::fromJson(libObj, filename, out->name);
|
||||||
if(lib->originalName.isEmpty())
|
if(lib->originalName.isEmpty())
|
||||||
@ -169,9 +169,9 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
|||||||
|
|
||||||
if (root.contains("+libraries"))
|
if (root.contains("+libraries"))
|
||||||
{
|
{
|
||||||
for (auto libVal : ensureArray(root.value("+libraries")))
|
for (auto libVal : requireArray(root.value("+libraries")))
|
||||||
{
|
{
|
||||||
QJsonObject libObj = ensureObject(libVal);
|
QJsonObject libObj = requireObject(libVal);
|
||||||
// parse the library
|
// parse the library
|
||||||
auto lib = RawLibrary::fromJsonPlus(libObj, filename);
|
auto lib = RawLibrary::fromJsonPlus(libObj, filename);
|
||||||
out->addLibs.append(lib);
|
out->addLibs.append(lib);
|
||||||
@ -180,10 +180,10 @@ VersionFilePtr VersionFile::fromJson(const QJsonDocument &doc, const QString &fi
|
|||||||
|
|
||||||
if (root.contains("-libraries"))
|
if (root.contains("-libraries"))
|
||||||
{
|
{
|
||||||
for (auto libVal : ensureArray(root.value("-libraries")))
|
for (auto libVal : requireArray(root.value("-libraries")))
|
||||||
{
|
{
|
||||||
auto libObj = ensureObject(libVal);
|
auto libObj = requireObject(libVal);
|
||||||
out->removeLibs.append(ensureString(libObj.value("name")));
|
out->removeLibs.append(requireString(libObj.value("name")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
|
Loading…
Reference in New Issue
Block a user