2014-03-02 23:42:04 +05:30
|
|
|
/**
|
|
|
|
* Some de-bullshitting for Qt JSON failures.
|
|
|
|
*
|
|
|
|
* Simple exception-throwing
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <QJsonValue>
|
|
|
|
#include <QJsonObject>
|
2014-03-05 06:20:05 +05:30
|
|
|
#include <QJsonDocument>
|
2014-03-02 23:42:04 +05:30
|
|
|
#include <QJsonArray>
|
2014-09-06 21:46:56 +05:30
|
|
|
#include <QFile>
|
|
|
|
#include <memory>
|
2014-03-02 23:42:04 +05:30
|
|
|
#include "MMCError.h"
|
|
|
|
|
|
|
|
class JSONValidationError : public MMCError
|
|
|
|
{
|
|
|
|
public:
|
2014-09-06 21:46:56 +05:30
|
|
|
JSONValidationError(QString cause) : MMCError(cause) {}
|
|
|
|
};
|
|
|
|
class FileOpenError : public MMCError
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileOpenError(const QFile &file) : MMCError(QObject::tr("Error opening %1: %2").arg(file.fileName(), file.errorString())) {}
|
2014-03-02 23:42:04 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
namespace MMCJson
|
|
|
|
{
|
2014-09-06 21:46:56 +05:30
|
|
|
/// parses the data into a json document. throws if there's a parse error
|
|
|
|
QJsonDocument parseDocument(const QByteArray &data, const QString &what);
|
|
|
|
|
|
|
|
/// tries to open and then parses the specified file. throws if there's an error
|
|
|
|
QJsonDocument parseFile(const QString &filename, const QString &what);
|
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
/// make sure the value exists. throw otherwise.
|
|
|
|
QJsonValue ensureExists(QJsonValue val, const QString what = "value");
|
|
|
|
|
|
|
|
/// make sure the value is converted into an object. throw otherwise.
|
|
|
|
QJsonObject ensureObject(const QJsonValue val, const QString what = "value");
|
|
|
|
|
2014-03-05 06:20:05 +05:30
|
|
|
/// make sure the document is converted into an object. throw otherwise.
|
2014-09-06 21:46:56 +05:30
|
|
|
QJsonObject ensureObject(const QJsonDocument val, const QString what = "document");
|
2014-03-05 06:20:05 +05:30
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
/// make sure the value is converted into an array. throw otherwise.
|
|
|
|
QJsonArray ensureArray(const QJsonValue val, QString what = "value");
|
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
/// make sure the document is converted into an array. throw otherwise.
|
|
|
|
QJsonArray ensureArray(const QJsonDocument &val, const QString &what = "document");
|
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
/// make sure the value is converted into a string. throw otherwise.
|
|
|
|
QString ensureString(const QJsonValue val, QString what = "value");
|
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
/// make sure the value is converted into a string that's parseable as an url. throw otherwise.
|
|
|
|
QUrl ensureUrl(const QJsonValue &val, const QString &what = "value");
|
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
/// make sure the value is converted into a boolean. throw otherwise.
|
|
|
|
bool ensureBoolean(const QJsonValue val, QString what = "value");
|
|
|
|
|
|
|
|
/// make sure the value is converted into an integer. throw otherwise.
|
|
|
|
int ensureInteger(const QJsonValue val, QString what = "value");
|
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
/// make sure the value is converted into an integer. throw otherwise. this version will return the default value if the field is undefined.
|
|
|
|
int ensureInteger(const QJsonValue val, QString what, const int def);
|
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
/// make sure the value is converted into a double precision floating number. throw otherwise.
|
|
|
|
double ensureDouble(const QJsonValue val, QString what = "value");
|
2014-05-11 16:07:21 +05:30
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
QStringList ensureStringList(const QJsonValue val, QString what);
|
|
|
|
|
2014-05-11 16:07:21 +05:30
|
|
|
void writeString(QJsonObject & to, QString key, QString value);
|
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
void writeStringList(QJsonObject & to, QString key, QStringList values);
|
2014-05-11 16:07:21 +05:30
|
|
|
|
|
|
|
template <typename T>
|
2014-09-06 21:46:56 +05:30
|
|
|
void writeObjectList(QJsonObject & to, QString key, QList<std::shared_ptr<T>> values)
|
2014-05-11 16:07:21 +05:30
|
|
|
{
|
2014-09-06 21:46:56 +05:30
|
|
|
if (!values.isEmpty())
|
2014-05-11 16:07:21 +05:30
|
|
|
{
|
|
|
|
QJsonArray array;
|
2014-09-06 21:46:56 +05:30
|
|
|
for (auto value: values)
|
2014-05-11 16:07:21 +05:30
|
|
|
{
|
|
|
|
array.append(value->toJson());
|
|
|
|
}
|
|
|
|
to.insert(key, array);
|
|
|
|
}
|
|
|
|
}
|
2014-09-06 21:46:56 +05:30
|
|
|
template <typename T>
|
|
|
|
void writeObjectList(QJsonObject & to, QString key, QList<T> values)
|
|
|
|
{
|
|
|
|
if (!values.isEmpty())
|
|
|
|
{
|
|
|
|
QJsonArray array;
|
|
|
|
for (auto value: values)
|
|
|
|
{
|
|
|
|
array.append(value.toJson());
|
|
|
|
}
|
|
|
|
to.insert(key, array);
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 23:42:04 +05:30
|
|
|
}
|
2014-05-11 16:07:21 +05:30
|
|
|
|