2014-03-02 23:42:04 +05:30
|
|
|
#include "MMCJson.h"
|
2014-09-06 21:46:56 +05:30
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
#include <QString>
|
2014-09-06 21:46:56 +05:30
|
|
|
#include <QUrl>
|
2014-05-11 20:13:20 +05:30
|
|
|
#include <QStringList>
|
2014-03-10 04:44:30 +05:30
|
|
|
#include <math.h>
|
2014-03-02 23:42:04 +05:30
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
QJsonDocument MMCJson::parseDocument(const QByteArray &data, const QString &what)
|
|
|
|
{
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
throw JSONValidationError(what + " is not valid JSON: " + error.errorString() + " at " + error.offset);
|
|
|
|
}
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
bool MMCJson::ensureBoolean(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isBool())
|
|
|
|
throw JSONValidationError(what + " is not boolean");
|
2014-09-06 21:46:56 +05:30
|
|
|
return val.toBool();
|
2014-03-02 23:42:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QJsonValue MMCJson::ensureExists(QJsonValue val, const QString what)
|
|
|
|
{
|
2014-05-11 16:07:21 +05:30
|
|
|
if(val.isUndefined() || val.isUndefined())
|
2014-03-02 23:42:04 +05:30
|
|
|
throw JSONValidationError(what + " does not exist");
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonArray MMCJson::ensureArray(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isArray())
|
|
|
|
throw JSONValidationError(what + " is not an array");
|
|
|
|
return val.toArray();
|
|
|
|
}
|
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
QJsonArray MMCJson::ensureArray(const QJsonDocument &val, const QString &what)
|
|
|
|
{
|
|
|
|
if (!val.isArray())
|
|
|
|
{
|
|
|
|
throw JSONValidationError(what + " is not an array");
|
|
|
|
}
|
|
|
|
return val.array();
|
|
|
|
}
|
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
double MMCJson::ensureDouble(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isDouble())
|
|
|
|
throw JSONValidationError(what + " is not a number");
|
2014-03-11 00:48:01 +05:30
|
|
|
return val.toDouble();
|
2014-03-02 23:42:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int MMCJson::ensureInteger(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
double ret = ensureDouble(val, what);
|
|
|
|
if (fmod(ret, 1) != 0)
|
|
|
|
throw JSONValidationError(what + " is not an integer");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject MMCJson::ensureObject(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isObject())
|
|
|
|
throw JSONValidationError(what + " is not an object");
|
|
|
|
return val.toObject();
|
|
|
|
}
|
|
|
|
|
2014-03-05 06:20:05 +05:30
|
|
|
QJsonObject MMCJson::ensureObject(const QJsonDocument val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isObject())
|
|
|
|
throw JSONValidationError(what + " is not an object");
|
|
|
|
return val.object();
|
|
|
|
}
|
|
|
|
|
2014-03-02 23:42:04 +05:30
|
|
|
QString MMCJson::ensureString(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isString())
|
|
|
|
throw JSONValidationError(what + " is not a string");
|
|
|
|
return val.toString();
|
|
|
|
}
|
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
QUrl MMCJson::ensureUrl(const QJsonValue &val, const QString &what)
|
|
|
|
{
|
|
|
|
const QUrl url = QUrl(ensureString(val, what));
|
|
|
|
if (!url.isValid())
|
|
|
|
{
|
|
|
|
throw JSONValidationError(what + " is not an url");
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument MMCJson::parseFile(const QString &filename, const QString &what)
|
|
|
|
{
|
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
throw FileOpenError(f);
|
|
|
|
}
|
|
|
|
return parseDocument(f.readAll(), what);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MMCJson::ensureInteger(const QJsonValue val, QString what, const int def)
|
|
|
|
{
|
|
|
|
if (val.isUndefined())
|
|
|
|
return def;
|
|
|
|
return ensureInteger(val, what);
|
|
|
|
}
|
|
|
|
|
2014-05-11 16:07:21 +05:30
|
|
|
void MMCJson::writeString(QJsonObject &to, QString key, QString value)
|
|
|
|
{
|
2014-09-06 21:46:56 +05:30
|
|
|
if (!value.isEmpty())
|
2014-05-11 16:07:21 +05:30
|
|
|
{
|
|
|
|
to.insert(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MMCJson::writeStringList(QJsonObject &to, QString key, QStringList values)
|
|
|
|
{
|
2014-09-06 21:46:56 +05:30
|
|
|
if (!values.isEmpty())
|
2014-05-11 16:07:21 +05:30
|
|
|
{
|
|
|
|
QJsonArray array;
|
|
|
|
for(auto value: values)
|
|
|
|
{
|
|
|
|
array.append(value);
|
|
|
|
}
|
|
|
|
to.insert(key, array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 21:46:56 +05:30
|
|
|
QStringList MMCJson::ensureStringList(const QJsonValue val, QString what)
|
|
|
|
{
|
|
|
|
const QJsonArray array = ensureArray(val, what);
|
|
|
|
QStringList out;
|
|
|
|
for (const auto value : array)
|
|
|
|
{
|
|
|
|
out.append(ensureString(value));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|