From dd093699ba8a5634962cc7a2d5afffa303b0dcb1 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 10 Oct 2017 12:19:31 +0900 Subject: 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(). --- jobs/converters.h | 88 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 28 deletions(-) (limited to 'jobs/converters.h') 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 -#include +#include +#include // Includes #include -#include namespace QMatrixClient { @@ -45,45 +44,78 @@ namespace QMatrixClient return QJsonArray::fromStringList(strings); } + template + struct FromJson + { + T operator()(QJsonValue jv) const { return static_cast(jv); } + }; + template inline T fromJson(const QJsonValue& jv) { - return QVariant(jv).value(); + return FromJson()(jv); } - template <> - inline int fromJson(const QJsonValue& jv) + template <> struct FromJson { - return jv.toInt(); - } + bool operator()(QJsonValue jv) const { return jv.toBool(); } + }; - template <> - inline qint64 fromJson(const QJsonValue& jv) + template <> struct FromJson { - return static_cast(jv.toDouble()); - } + int operator()(QJsonValue jv) const { return jv.toInt(); } + }; - template <> - inline double fromJson(const QJsonValue& jv) + template <> struct FromJson { - return jv.toDouble(); - } + double operator()(QJsonValue jv) const { return jv.toDouble(); } + }; - template <> - inline QString fromJson(const QJsonValue& jv) + template <> struct FromJson { - return jv.toString(); - } + qint64 operator()(QJsonValue jv) const { return qint64(jv.toDouble()); } + }; - template <> - inline QDateTime fromJson(const QJsonValue& jv) + template <> struct FromJson { - return QDateTime::fromMSecsSinceEpoch(fromJson(jv), Qt::UTC); - } + QString operator()(QJsonValue jv) const { return jv.toString(); } + }; - template <> - inline QDate fromJson(const QJsonValue& jv) + template <> struct FromJson { - return fromJson(jv).date(); - } + QDateTime operator()(QJsonValue jv) const + { + return QDateTime::fromMSecsSinceEpoch(fromJson(jv), Qt::UTC); + } + }; + + template <> struct FromJson + { + QDate operator()(QJsonValue jv) const + { + return fromJson(jv).date(); + } + }; + + template <> struct FromJson + { + QJsonObject operator()(QJsonValue jv) const { return jv.toObject(); } + }; + + template <> struct FromJson + { + QJsonArray operator()(QJsonValue jv) const { return jv.toArray(); } + }; + + template struct FromJson> + { + QVector operator()(QJsonValue jv) const + { + const auto jsonArray = jv.toArray(); + QVector vect; vect.resize(jsonArray.size()); + std::transform(jsonArray.begin(), jsonArray.end(), + vect.begin(), FromJson()); + return vect; + } + }; } // namespace QMatrixClient -- cgit v1.2.3