2014-03-02 18:12:04 +00:00
|
|
|
#include "MMCJson.h"
|
|
|
|
#include <QString>
|
2014-03-09 23:14:30 +00:00
|
|
|
#include <math.h>
|
2014-03-02 18:12:04 +00:00
|
|
|
|
|
|
|
bool MMCJson::ensureBoolean(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isBool())
|
|
|
|
throw JSONValidationError(what + " is not boolean");
|
|
|
|
return val.isBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonValue MMCJson::ensureExists(QJsonValue val, const QString what)
|
|
|
|
{
|
2014-05-11 11:37:21 +01:00
|
|
|
if(val.isUndefined() || val.isUndefined())
|
2014-03-02 18:12:04 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
double MMCJson::ensureDouble(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isDouble())
|
|
|
|
throw JSONValidationError(what + " is not a number");
|
2014-03-10 19:18:01 +00:00
|
|
|
return val.toDouble();
|
2014-03-02 18:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 00:50:05 +00:00
|
|
|
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 18:12:04 +00:00
|
|
|
QString MMCJson::ensureString(const QJsonValue val, const QString what)
|
|
|
|
{
|
|
|
|
if (!val.isString())
|
|
|
|
throw JSONValidationError(what + " is not a string");
|
|
|
|
return val.toString();
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:37:21 +01:00
|
|
|
void MMCJson::writeString(QJsonObject &to, QString key, QString value)
|
|
|
|
{
|
|
|
|
if(value.size())
|
|
|
|
{
|
|
|
|
to.insert(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MMCJson::writeStringList(QJsonObject &to, QString key, QStringList values)
|
|
|
|
{
|
|
|
|
if(values.size())
|
|
|
|
{
|
|
|
|
QJsonArray array;
|
|
|
|
for(auto value: values)
|
|
|
|
{
|
|
|
|
array.append(value);
|
|
|
|
}
|
|
|
|
to.insert(key, array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|