aboutsummaryrefslogtreecommitdiff
path: root/jobs/converters.h
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-10 12:19:31 +0900
commitdd093699ba8a5634962cc7a2d5afffa303b0dcb1 (patch)
tree1f55bdb1c30fff4536c0851be361c9c1bf322367 /jobs/converters.h
parentc0953cf02f18d3ce6d514af1d22b54a4612bd139 (diff)
downloadlibquotient-dd093699ba8a5634962cc7a2d5afffa303b0dcb1.tar.gz
libquotient-dd093699ba8a5634962cc7a2d5afffa303b0dcb1.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/converters.h')
-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