aboutsummaryrefslogtreecommitdiff
path: root/jobs
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-10 12:19:31 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-13 10:37:18 +0200
commit8207ef24c0e71133f3cfe115a4652767b4a63f9d (patch)
treec711bc8c135227d768321fc4d8fe24be8190fd6a /jobs
parentb2a017303a34e248873b2e47e560a41dc4c5111c (diff)
downloadlibquotient-8207ef24c0e71133f3cfe115a4652767b4a63f9d.tar.gz
libquotient-8207ef24c0e71133f3cfe115a4652767b4a63f9d.zip
Extend the number of types supported by fromJson<>()
Template function cannot have partial specializations, and we need to deserialise QVector<> objects. So fromJson<>() is now a wrapper around FromJson<> template class that does all the dispatching stuff in its operator().
Diffstat (limited to 'jobs')
-rw-r--r--jobs/converters.h88
1 files changed, 60 insertions, 28 deletions
diff --git a/jobs/converters.h b/jobs/converters.h
index f9ab0269..f6e850c6 100644
--- a/jobs/converters.h
+++ b/jobs/converters.h
@@ -18,10 +18,9 @@
#pragma once
-#include <QtCore/QJsonValue>
-#include <QtCore/QJsonArray>
+#include <QtCore/QJsonObject>
+#include <QtCore/QJsonArray> // Includes <QtCore/QJsonValue>
#include <QtCore/QDate>
-#include <QtCore/QVariant>
namespace QMatrixClient
{
@@ -46,44 +45,77 @@ namespace QMatrixClient
}
template <typename T>
+ struct FromJson
+ {
+ T operator()(QJsonValue jv) const { return static_cast<T>(jv); }
+ };
+
+ template <typename T>
inline T fromJson(const QJsonValue& jv)
{
- return QVariant(jv).value<T>();
+ return FromJson<T>()(jv);
}
- template <>
- inline int fromJson<int>(const QJsonValue& jv)
+ template <> struct FromJson<bool>
{
- return jv.toInt();
- }
+ bool operator()(QJsonValue jv) const { return jv.toBool(); }
+ };
- template <>
- inline qint64 fromJson<qint64>(const QJsonValue& jv)
+ template <> struct FromJson<int>
{
- return static_cast<qint64>(jv.toDouble());
- }
+ int operator()(QJsonValue jv) const { return jv.toInt(); }
+ };
- template <>
- inline double fromJson<double>(const QJsonValue& jv)
+ template <> struct FromJson<double>
{
- return jv.toDouble();
- }
+ double operator()(QJsonValue jv) const { return jv.toDouble(); }
+ };
- template <>
- inline QString fromJson<QString>(const QJsonValue& jv)
+ template <> struct FromJson<qint64>
{
- return jv.toString();
- }
+ qint64 operator()(QJsonValue jv) const { return qint64(jv.toDouble()); }
+ };
- template <>
- inline QDateTime fromJson<QDateTime>(const QJsonValue& jv)
+ template <> struct FromJson<QString>
{
- return QDateTime::fromMSecsSinceEpoch(fromJson<qint64>(jv), Qt::UTC);
- }
+ QString operator()(QJsonValue jv) const { return jv.toString(); }
+ };
- template <>
- inline QDate fromJson<QDate>(const QJsonValue& jv)
+ template <> struct FromJson<QDateTime>
{
- return fromJson<QDateTime>(jv).date();
- }
+ QDateTime operator()(QJsonValue jv) const
+ {
+ return QDateTime::fromMSecsSinceEpoch(fromJson<qint64>(jv), Qt::UTC);
+ }
+ };
+
+ template <> struct FromJson<QDate>
+ {
+ QDate operator()(QJsonValue jv) const
+ {
+ return fromJson<QDateTime>(jv).date();
+ }
+ };
+
+ template <> struct FromJson<QJsonObject>
+ {
+ QJsonObject operator()(QJsonValue jv) const { return jv.toObject(); }
+ };
+
+ template <> struct FromJson<QJsonArray>
+ {
+ QJsonArray operator()(QJsonValue jv) const { return jv.toArray(); }
+ };
+
+ template <typename T> struct FromJson<QVector<T>>
+ {
+ QVector<T> operator()(QJsonValue jv) const
+ {
+ const auto jsonArray = jv.toArray();
+ QVector<T> vect; vect.resize(jsonArray.size());
+ std::transform(jsonArray.begin(), jsonArray.end(),
+ vect.begin(), FromJson<T>());
+ return vect;
+ }
+ };
} // namespace QMatrixClient