Remove traces of ensure helpers
Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
@@ -287,7 +287,7 @@ QStringList toStringList(const QString& jsonString)
|
|||||||
if (parseError.error != QJsonParseError::NoError || !doc.isArray())
|
if (parseError.error != QJsonParseError::NoError || !doc.isArray())
|
||||||
return {};
|
return {};
|
||||||
try {
|
try {
|
||||||
return ensureIsArrayOf<QString>(doc.array(), "");
|
return requireIsArrayOf<QString>(doc);
|
||||||
} catch (Json::JsonException& e) {
|
} catch (Json::JsonException& e) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,18 +153,6 @@ 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
|
||||||
template <typename T>
|
|
||||||
T ensureIsType(const QJsonValue& value, const T default_ = T(), const QString& what = "Value")
|
|
||||||
{
|
|
||||||
if (value.isUndefined() || value.isNull()) {
|
|
||||||
return default_;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return requireIsType<T>(value, what);
|
|
||||||
} catch (const JsonException&) {
|
|
||||||
return default_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -177,16 +165,6 @@ T requireIsType(const QJsonObject& parent, const QString& key, const QString& wh
|
|||||||
return requireIsType<T>(parent.value(key), localWhat);
|
return requireIsType<T>(parent.value(key), localWhat);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
T ensureIsType(const QJsonObject& parent, const QString& key, const T default_ = T(), const QString& what = "__placeholder__")
|
|
||||||
{
|
|
||||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
|
||||||
if (!parent.contains(key)) {
|
|
||||||
return default_;
|
|
||||||
}
|
|
||||||
return ensureIsType<T>(parent.value(key), default_, localWhat);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
QList<T> requireIsArrayOf(const QJsonDocument& doc)
|
QList<T> requireIsArrayOf(const QJsonDocument& doc)
|
||||||
{
|
{
|
||||||
@@ -198,26 +176,6 @@ QList<T> requireIsArrayOf(const QJsonDocument& doc)
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
QList<T> ensureIsArrayOf(const QJsonValue& value, const QString& what = "Value")
|
|
||||||
{
|
|
||||||
const QJsonArray array = ensureIsType<QJsonArray>(value, QJsonArray(), what);
|
|
||||||
QList<T> out;
|
|
||||||
for (const QJsonValue val : array) {
|
|
||||||
out.append(requireIsType<T>(val, what));
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
QList<T> ensureIsArrayOf(const QJsonValue& value, const QList<T> default_, const QString& what = "Value")
|
|
||||||
{
|
|
||||||
if (value.isUndefined()) {
|
|
||||||
return default_;
|
|
||||||
}
|
|
||||||
return ensureIsArrayOf<T>(value, what);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// @throw JsonException
|
/// @throw JsonException
|
||||||
template <typename T>
|
template <typename T>
|
||||||
QList<T> requireIsArrayOf(const QJsonObject& parent, const QString& key, const QString& what = "__placeholder__")
|
QList<T> requireIsArrayOf(const QJsonObject& parent, const QString& key, const QString& what = "__placeholder__")
|
||||||
@@ -226,20 +184,13 @@ QList<T> requireIsArrayOf(const QJsonObject& parent, const QString& key, const Q
|
|||||||
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), localWhat);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
const QJsonArray array = parent[key].toArray();
|
||||||
QList<T> ensureIsArrayOf(const QJsonObject& parent,
|
QList<T> out;
|
||||||
const QString& key,
|
for (const QJsonValue val : array) {
|
||||||
const QList<T>& default_ = QList<T>(),
|
out.append(requireIsType<T>(val, "Document"));
|
||||||
const QString& what = "__placeholder__")
|
|
||||||
{
|
|
||||||
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
|
||||||
if (!parent.contains(key)) {
|
|
||||||
return default_;
|
|
||||||
}
|
}
|
||||||
return ensureIsArrayOf<T>(parent.value(key), default_, localWhat);
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -251,7 +202,7 @@ QList<T> ensureIsArrayOf(const QJsonObject& parent,
|
|||||||
inline TYPE require##NAME(const QJsonObject& parent, const QString& key, const QString& what = "__placeholder__") \
|
inline TYPE require##NAME(const QJsonObject& parent, const QString& key, const QString& what = "__placeholder__") \
|
||||||
{ \
|
{ \
|
||||||
return requireIsType<TYPE>(parent, key, what); \
|
return requireIsType<TYPE>(parent, key, what); \
|
||||||
} \
|
}
|
||||||
|
|
||||||
JSON_HELPERFUNCTIONS(Array, QJsonArray)
|
JSON_HELPERFUNCTIONS(Array, QJsonArray)
|
||||||
JSON_HELPERFUNCTIONS(Object, QJsonObject)
|
JSON_HELPERFUNCTIONS(Object, QJsonObject)
|
||||||
|
|||||||
Reference in New Issue
Block a user