2015-05-28 23:08:29 +05:30
|
|
|
// Licensed under the Apache-2.0 license. See README.md for details.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QUuid>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "Exception.h"
|
|
|
|
|
|
|
|
namespace Json
|
|
|
|
{
|
2015-09-05 22:16:57 +05:30
|
|
|
class MULTIMC_LOGIC_EXPORT JsonException : public ::Exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JsonException(const QString &message) : Exception(message) {}
|
|
|
|
};
|
2015-05-28 23:08:29 +05:30
|
|
|
|
2015-06-04 00:41:03 +05:30
|
|
|
/// @throw FileSystemException
|
2015-05-28 23:08:29 +05:30
|
|
|
void write(const QJsonDocument &doc, const QString &filename);
|
2015-06-04 00:41:03 +05:30
|
|
|
/// @throw FileSystemException
|
2015-05-28 23:08:29 +05:30
|
|
|
void write(const QJsonObject &object, const QString &filename);
|
2015-06-04 00:41:03 +05:30
|
|
|
/// @throw FileSystemException
|
2015-05-28 23:08:29 +05:30
|
|
|
void write(const QJsonArray &array, const QString &filename);
|
2015-06-04 00:41:03 +05:30
|
|
|
|
2015-05-28 23:08:29 +05:30
|
|
|
QByteArray toBinary(const QJsonObject &obj);
|
|
|
|
QByteArray toBinary(const QJsonArray &array);
|
|
|
|
QByteArray toText(const QJsonObject &obj);
|
|
|
|
QByteArray toText(const QJsonArray &array);
|
|
|
|
|
2015-06-04 00:41:03 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 09:15:49 +05:30
|
|
|
MULTIMC_LOGIC_EXPORT QJsonDocument requireDocument(const QByteArray &data, const QString &what = "Document");
|
2015-06-04 00:41:03 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 09:15:49 +05:30
|
|
|
MULTIMC_LOGIC_EXPORT QJsonDocument requireDocument(const QString &filename, const QString &what = "Document");
|
2015-06-04 00:41:03 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 09:15:49 +05:30
|
|
|
MULTIMC_LOGIC_EXPORT QJsonObject requireObject(const QJsonDocument &doc, const QString &what = "Document");
|
2015-06-04 00:41:03 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 09:15:49 +05:30
|
|
|
MULTIMC_LOGIC_EXPORT QJsonArray requireArray(const QJsonDocument &doc, const QString &what = "Document");
|
2015-05-28 23:08:29 +05:30
|
|
|
|
|
|
|
/////////////////// WRITING ////////////////////
|
|
|
|
|
|
|
|
void writeString(QJsonObject & to, const QString &key, const QString &value);
|
|
|
|
void writeStringList(QJsonObject & to, const QString &key, const QStringList &values);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QJsonValue toJson(const T &t)
|
|
|
|
{
|
|
|
|
return QJsonValue(t);
|
|
|
|
}
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QUrl>(const QUrl &url);
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QByteArray>(const QByteArray &data);
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QDateTime>(const QDateTime &datetime);
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QDir>(const QDir &dir);
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QUuid>(const QUuid &uuid);
|
|
|
|
template<>
|
|
|
|
QJsonValue toJson<QVariant>(const QVariant &variant);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
QJsonArray toJsonArray(const QList<T> &container)
|
|
|
|
{
|
|
|
|
QJsonArray array;
|
|
|
|
for (const T item : container)
|
|
|
|
{
|
|
|
|
array.append(toJson<T>(item));
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////// READING ////////////////////
|
|
|
|
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-05-28 23:08:29 +05:30
|
|
|
template <typename T>
|
2015-06-04 01:27:22 +05:30
|
|
|
T requireIsType(const QJsonValue &value, const QString &what = "Value");
|
|
|
|
|
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT double requireIsType<double>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT bool requireIsType<bool>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT int requireIsType<int>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QJsonObject requireIsType<QJsonObject>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QJsonArray requireIsType<QJsonArray>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QJsonValue requireIsType<QJsonValue>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QByteArray requireIsType<QByteArray>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QDateTime requireIsType<QDateTime>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QVariant requireIsType<QVariant>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QString requireIsType<QString>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QUuid requireIsType<QUuid>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QDir requireIsType<QDir>(const QJsonValue &value, const QString &what);
|
2015-06-04 01:27:22 +05:30
|
|
|
/// @throw JsonException
|
2015-12-28 10:17:11 +05:30
|
|
|
template<> MULTIMC_LOGIC_EXPORT QUrl requireIsType<QUrl>(const QJsonValue &value, const QString &what);
|
2015-05-28 23:08:29 +05:30
|
|
|
|
|
|
|
// the following functions are higher level functions, that make use of the above functions for
|
|
|
|
// type conversion
|
|
|
|
template <typename T>
|
2016-04-07 02:39:30 +05:30
|
|
|
T ensureIsType(const QJsonValue &value, const T default_ = T(), const QString &what = "Value")
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2016-04-07 02:39:30 +05:30
|
|
|
if (value.isUndefined() || value.isNull())
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
return default_;
|
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
try
|
|
|
|
{
|
|
|
|
return requireIsType<T>(value, what);
|
|
|
|
}
|
|
|
|
catch (JsonException &)
|
|
|
|
{
|
|
|
|
return default_;
|
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 00:41:03 +05:30
|
|
|
|
|
|
|
/// @throw JsonException
|
2015-05-28 23:08:29 +05:30
|
|
|
template <typename T>
|
2015-06-04 01:27:22 +05:30
|
|
|
T requireIsType(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__")
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
|
|
|
if (!parent.contains(key))
|
|
|
|
{
|
|
|
|
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
return requireIsType<T>(parent.value(key), localWhat);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 00:41:03 +05:30
|
|
|
|
2015-05-28 23:08:29 +05:30
|
|
|
template <typename T>
|
2016-04-07 02:39:30 +05:30
|
|
|
T ensureIsType(const QJsonObject &parent, const QString &key, const T default_ = T(), const QString &what = "__placeholder__")
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
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>
|
2016-04-07 02:39:30 +05:30
|
|
|
QVector<T> requireIsArrayOf(const QJsonDocument &doc)
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2015-06-04 01:27:22 +05:30
|
|
|
const QJsonArray array = requireArray(doc);
|
2016-04-07 02:39:30 +05:30
|
|
|
QVector<T> out;
|
2015-05-28 23:08:29 +05:30
|
|
|
for (const QJsonValue val : array)
|
|
|
|
{
|
2015-06-04 01:27:22 +05:30
|
|
|
out.append(requireIsType<T>(val, "Document"));
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2015-06-04 00:41:03 +05:30
|
|
|
|
2015-05-28 23:08:29 +05:30
|
|
|
template <typename T>
|
2016-04-07 02:39:30 +05:30
|
|
|
QVector<T> ensureIsArrayOf(const QJsonValue &value, const QString &what = "Value")
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
2016-04-07 02:39:30 +05:30
|
|
|
const QJsonArray array = ensureIsType<QJsonArray>(value, QJsonArray(), what);
|
|
|
|
QVector<T> out;
|
2015-05-28 23:08:29 +05:30
|
|
|
for (const QJsonValue val : array)
|
|
|
|
{
|
2016-04-07 02:39:30 +05:30
|
|
|
out.append(requireIsType<T>(val, what));
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2015-06-04 00:41:03 +05:30
|
|
|
|
2015-05-28 23:08:29 +05:30
|
|
|
template <typename T>
|
2016-04-07 02:39:30 +05:30
|
|
|
QVector<T> ensureIsArrayOf(const QJsonValue &value, const QVector<T> default_, const QString &what = "Value")
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
if (value.isUndefined())
|
|
|
|
{
|
|
|
|
return default_;
|
|
|
|
}
|
2015-06-04 01:27:22 +05:30
|
|
|
return ensureIsArrayOf<T>(value, what);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 00:41:03 +05:30
|
|
|
|
|
|
|
/// @throw JsonException
|
2015-05-28 23:08:29 +05:30
|
|
|
template <typename T>
|
2016-04-07 02:39:30 +05:30
|
|
|
QVector<T> requireIsArrayOf(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__")
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
|
|
|
if (!parent.contains(key))
|
|
|
|
{
|
|
|
|
throw JsonException(localWhat + "s parent does not contain " + localWhat);
|
|
|
|
}
|
2016-04-07 02:39:30 +05:30
|
|
|
return ensureIsArrayOf<T>(parent.value(key), localWhat);
|
2015-05-28 23:08:29 +05:30
|
|
|
}
|
2015-06-04 00:41:03 +05:30
|
|
|
|
2015-05-28 23:08:29 +05:30
|
|
|
template <typename T>
|
2016-04-07 02:39:30 +05:30
|
|
|
QVector<T> ensureIsArrayOf(const QJsonObject &parent, const QString &key,
|
|
|
|
const QVector<T> &default_ = QVector<T>(), const QString &what = "__placeholder__")
|
2015-05-28 23:08:29 +05:30
|
|
|
{
|
|
|
|
const QString localWhat = QString(what).replace("__placeholder__", '\'' + key + '\'');
|
|
|
|
if (!parent.contains(key))
|
|
|
|
{
|
|
|
|
return default_;
|
|
|
|
}
|
|
|
|
return ensureIsArrayOf<T>(parent.value(key), default_, localWhat);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) \
|
2015-06-04 01:27:22 +05:30
|
|
|
inline TYPE require##NAME(const QJsonValue &value, const QString &what = "Value") \
|
|
|
|
{ \
|
|
|
|
return requireIsType<TYPE>(value, what); \
|
|
|
|
} \
|
2016-04-07 02:39:30 +05:30
|
|
|
inline TYPE ensure##NAME(const QJsonValue &value, const TYPE default_ = TYPE(), const QString &what = "Value") \
|
2015-06-04 01:27:22 +05:30
|
|
|
{ \
|
|
|
|
return ensureIsType<TYPE>(value, default_, what); \
|
|
|
|
} \
|
|
|
|
inline TYPE require##NAME(const QJsonObject &parent, const QString &key, const QString &what = "__placeholder__") \
|
|
|
|
{ \
|
|
|
|
return requireIsType<TYPE>(parent, key, what); \
|
|
|
|
} \
|
2016-04-07 02:39:30 +05:30
|
|
|
inline TYPE ensure##NAME(const QJsonObject &parent, const QString &key, const TYPE default_ = TYPE(), const QString &what = "__placeholder") \
|
2015-06-04 01:27:22 +05:30
|
|
|
{ \
|
|
|
|
return ensureIsType<TYPE>(parent, key, default_, what); \
|
|
|
|
}
|
2015-05-28 23:08:29 +05:30
|
|
|
|
|
|
|
JSON_HELPERFUNCTIONS(Array, QJsonArray)
|
|
|
|
JSON_HELPERFUNCTIONS(Object, QJsonObject)
|
|
|
|
JSON_HELPERFUNCTIONS(JsonValue, QJsonValue)
|
|
|
|
JSON_HELPERFUNCTIONS(String, QString)
|
|
|
|
JSON_HELPERFUNCTIONS(Boolean, bool)
|
|
|
|
JSON_HELPERFUNCTIONS(Double, double)
|
|
|
|
JSON_HELPERFUNCTIONS(Integer, int)
|
|
|
|
JSON_HELPERFUNCTIONS(DateTime, QDateTime)
|
|
|
|
JSON_HELPERFUNCTIONS(Url, QUrl)
|
|
|
|
JSON_HELPERFUNCTIONS(ByteArray, QByteArray)
|
|
|
|
JSON_HELPERFUNCTIONS(Dir, QDir)
|
|
|
|
JSON_HELPERFUNCTIONS(Uuid, QUuid)
|
|
|
|
JSON_HELPERFUNCTIONS(Variant, QVariant)
|
|
|
|
|
|
|
|
#undef JSON_HELPERFUNCTIONS
|
|
|
|
|
|
|
|
}
|
|
|
|
using JSONValidationError = Json::JsonException;
|