Added utils to core lib
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#include "jsonutils.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
template<>
|
||||
QJsonDocument getJson<QJsonDocument>(const QJsonDocument &document)
|
||||
{
|
||||
return document;
|
||||
}
|
||||
|
||||
template<>
|
||||
QJsonObject getJson<QJsonObject>(const QJsonDocument &document)
|
||||
{
|
||||
if(!document.isObject())
|
||||
throw std::runtime_error("JSON document does not contain an object!");
|
||||
|
||||
return document.object();
|
||||
}
|
||||
|
||||
template<>
|
||||
QJsonArray getJson<QJsonArray>(const QJsonDocument &document)
|
||||
{
|
||||
if(!document.isArray())
|
||||
throw std::runtime_error("JSON document does not contain an object!");
|
||||
|
||||
return document.array();
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include "dbcorelib_global.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonParseError>
|
||||
#include <QFile>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
template<typename T>
|
||||
T getJson(const QJsonDocument &document);
|
||||
|
||||
template<>
|
||||
QJsonDocument DBCORELIB_EXPORT getJson<QJsonDocument>(const QJsonDocument &document);
|
||||
|
||||
template<>
|
||||
QJsonObject DBCORELIB_EXPORT getJson<QJsonObject>(const QJsonDocument &document);
|
||||
|
||||
template<>
|
||||
QJsonArray DBCORELIB_EXPORT getJson<QJsonArray>(const QJsonDocument &document);
|
||||
|
||||
template<typename T>
|
||||
T getJson(const QByteArray &byteArray);
|
||||
|
||||
template<typename T>
|
||||
T getJson(QIODevice &device);
|
||||
|
||||
template<typename T>
|
||||
T getJson(const QString &filename);
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
T getJson(const QByteArray &byteArray)
|
||||
{
|
||||
QJsonParseError error;
|
||||
auto document = QJsonDocument::fromJson(byteArray, &error);
|
||||
if(error.error != QJsonParseError::NoError)
|
||||
throw std::runtime_error(QString("Could not parse json: %0").arg(error.errorString()).toStdString());
|
||||
|
||||
return getJson<T>(document);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T getJson(QIODevice &device)
|
||||
{
|
||||
return getJson<T>(device.readAll());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T getJson(const QString &filename)
|
||||
{
|
||||
QFile file(filename);
|
||||
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
|
||||
throw std::runtime_error(QString("Could not open json file %0: %1").arg(filename, file.errorString()).toStdString());
|
||||
|
||||
return getJson<T>(file);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "timeutils.h"
|
||||
|
||||
int timeToSeconds(const QTime &time)
|
||||
{
|
||||
return QTime(0, 0).secsTo(time);
|
||||
}
|
||||
|
||||
QTime timeBetween(const QTime &l, const QTime &r)
|
||||
{
|
||||
Q_ASSERT(l <= r);
|
||||
return QTime(0, 0).addSecs(l.secsTo(r));
|
||||
}
|
||||
|
||||
QTime timeAdd(const QTime &l, const QTime &r)
|
||||
{
|
||||
Q_ASSERT(timeToSeconds(l) + timeToSeconds(r) < 86400);
|
||||
return l.addSecs(QTime(0, 0).secsTo(r));
|
||||
}
|
||||
|
||||
QTime timeNormalise(const QTime &time)
|
||||
{
|
||||
return QTime(time.hour(), time.minute());
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <QTime>
|
||||
|
||||
#include "dbcorelib_global.h"
|
||||
|
||||
int DBCORELIB_EXPORT timeToSeconds(const QTime &time);
|
||||
QTime DBCORELIB_EXPORT timeBetween(const QTime &l, const QTime &r);
|
||||
QTime DBCORELIB_EXPORT timeAdd(const QTime &l, const QTime &r);
|
||||
QTime DBCORELIB_EXPORT timeNormalise(const QTime &time);
|
||||
Reference in New Issue
Block a user