diff options
Diffstat (limited to 'converters.h')
-rw-r--r-- | converters.h | 68 |
1 files changed, 48 insertions, 20 deletions
diff --git a/converters.h b/converters.h index 0d7f734e..bba298e0 100644 --- a/converters.h +++ b/converters.h @@ -24,14 +24,16 @@ namespace QMatrixClient { - template <typename T> - inline QJsonValue toJson(T&& val) - { - return QJsonValue(std::forward<T>(val)); - } + // This catches anything implicitly convertible to QJsonValue/Object/Array + inline QJsonValue toJson(const QJsonValue& val) { return val; } + inline QJsonObject toJson(const QJsonObject& o) { return o; } + inline QJsonArray toJson(const QJsonArray& arr) { return arr; } +#ifdef _MSC_VER // MSVC gets lost and doesn't know which overload to use + inline QJsonValue toJson(const QString& s) { return s; } +#endif template <typename T> - inline QJsonValue toJson(const QVector<T>& vals) + inline QJsonArray toJson(const QVector<T>& vals) { QJsonArray ar; for (const auto& v: vals) @@ -39,7 +41,7 @@ namespace QMatrixClient return ar; } - inline QJsonValue toJson(const QStringList& strings) + inline QJsonArray toJson(const QStringList& strings) { return QJsonArray::fromStringList(strings); } @@ -50,9 +52,18 @@ namespace QMatrixClient } template <typename T> + inline QJsonObject toJson(const QHash<QString, T>& hashMap) + { + QJsonObject json; + for (auto it = hashMap.begin(); it != hashMap.end(); ++it) + json.insert(it.key(), toJson(it.value())); + return json; + } + + template <typename T> struct FromJson { - T operator()(QJsonValue jv) const { return static_cast<T>(jv); } + T operator()(const QJsonValue& jv) const { return static_cast<T>(jv); } }; template <typename T> @@ -63,32 +74,32 @@ namespace QMatrixClient template <> struct FromJson<bool> { - bool operator()(QJsonValue jv) const { return jv.toBool(); } + bool operator()(const QJsonValue& jv) const { return jv.toBool(); } }; template <> struct FromJson<int> { - int operator()(QJsonValue jv) const { return jv.toInt(); } + int operator()(const QJsonValue& jv) const { return jv.toInt(); } }; template <> struct FromJson<double> { - double operator()(QJsonValue jv) const { return jv.toDouble(); } + double operator()(const QJsonValue& jv) const { return jv.toDouble(); } }; template <> struct FromJson<qint64> { - qint64 operator()(QJsonValue jv) const { return qint64(jv.toDouble()); } + qint64 operator()(const QJsonValue& jv) const { return qint64(jv.toDouble()); } }; template <> struct FromJson<QString> { - QString operator()(QJsonValue jv) const { return jv.toString(); } + QString operator()(const QJsonValue& jv) const { return jv.toString(); } }; template <> struct FromJson<QDateTime> { - QDateTime operator()(QJsonValue jv) const + QDateTime operator()(const QJsonValue& jv) const { return QDateTime::fromMSecsSinceEpoch(fromJson<qint64>(jv), Qt::UTC); } @@ -96,7 +107,7 @@ namespace QMatrixClient template <> struct FromJson<QDate> { - QDate operator()(QJsonValue jv) const + QDate operator()(const QJsonValue& jv) const { return fromJson<QDateTime>(jv).date(); } @@ -104,17 +115,23 @@ namespace QMatrixClient template <> struct FromJson<QJsonObject> { - QJsonObject operator()(QJsonValue jv) const { return jv.toObject(); } + QJsonObject operator()(const QJsonValue& jv) const + { + return jv.toObject(); + } }; template <> struct FromJson<QJsonArray> { - QJsonArray operator()(QJsonValue jv) const { return jv.toArray(); } + QJsonArray operator()(const QJsonValue& jv) const + { + return jv.toArray(); + } }; template <typename T> struct FromJson<QVector<T>> { - QVector<T> operator()(QJsonValue jv) const + QVector<T> operator()(const QJsonValue& jv) const { const auto jsonArray = jv.toArray(); QVector<T> vect; vect.resize(jsonArray.size()); @@ -126,7 +143,7 @@ namespace QMatrixClient template <typename T> struct FromJson<QList<T>> { - QList<T> operator()(QJsonValue jv) const + QList<T> operator()(const QJsonValue& jv) const { const auto jsonArray = jv.toArray(); QList<T> sl; sl.reserve(jsonArray.size()); @@ -140,10 +157,21 @@ namespace QMatrixClient template <> struct FromJson<QByteArray> { - QByteArray operator()(QJsonValue jv) const + inline QByteArray operator()(const QJsonValue& jv) const { return fromJson<QString>(jv).toLatin1(); } }; + template <typename T> struct FromJson<QHash<QString, T>> + { + QHash<QString, T> operator()(const QJsonValue& jv) const + { + const auto json = jv.toObject(); + QHash<QString, T> h; h.reserve(json.size()); + for (auto it = json.begin(); it != json.end(); ++it) + h.insert(it.key(), fromJson<T>(it.value())); + return h; + } + }; } // namespace QMatrixClient |