aboutsummaryrefslogtreecommitdiff
path: root/lib/converters.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/converters.h')
-rw-r--r--lib/converters.h70
1 files changed, 53 insertions, 17 deletions
diff --git a/lib/converters.h b/lib/converters.h
index cfe9c01c..68005c0d 100644
--- a/lib/converters.h
+++ b/lib/converters.h
@@ -23,6 +23,20 @@
#include <QtCore/QDate>
#include <QtCore/QVariant>
+#include <unordered_map>
+
+// Enable std::unordered_map<QString, T>
+namespace std
+{
+ template <> struct hash<QString>
+ {
+ size_t operator()(const QString& s) const Q_DECL_NOEXCEPT
+ {
+ return qHash(s, uint(qGlobalQHashSeed()));
+ }
+ };
+}
+
namespace QMatrixClient
{
// This catches anything implicitly convertible to QJsonValue/Object/Array
@@ -83,6 +97,15 @@ namespace QMatrixClient
}
template <typename T>
+ inline QJsonObject toJson(const std::unordered_map<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()(const QJsonValue& jv) const { return static_cast<T>(jv); }
@@ -96,32 +119,32 @@ namespace QMatrixClient
template <> struct FromJson<bool>
{
- bool operator()(const QJsonValue& jv) const { return jv.toBool(); }
+ auto operator()(const QJsonValue& jv) const { return jv.toBool(); }
};
template <> struct FromJson<int>
{
- int operator()(const QJsonValue& jv) const { return jv.toInt(); }
+ auto operator()(const QJsonValue& jv) const { return jv.toInt(); }
};
template <> struct FromJson<double>
{
- double operator()(const QJsonValue& jv) const { return jv.toDouble(); }
+ auto operator()(const QJsonValue& jv) const { return jv.toDouble(); }
};
template <> struct FromJson<qint64>
{
- qint64 operator()(const QJsonValue& jv) const { return qint64(jv.toDouble()); }
+ auto operator()(const QJsonValue& jv) const { return qint64(jv.toDouble()); }
};
template <> struct FromJson<QString>
{
- QString operator()(const QJsonValue& jv) const { return jv.toString(); }
+ auto operator()(const QJsonValue& jv) const { return jv.toString(); }
};
template <> struct FromJson<QDateTime>
{
- QDateTime operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
return QDateTime::fromMSecsSinceEpoch(fromJson<qint64>(jv), Qt::UTC);
}
@@ -129,7 +152,7 @@ namespace QMatrixClient
template <> struct FromJson<QDate>
{
- QDate operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
return fromJson<QDateTime>(jv).date();
}
@@ -137,7 +160,7 @@ namespace QMatrixClient
template <> struct FromJson<QJsonObject>
{
- QJsonObject operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
return jv.toObject();
}
@@ -145,7 +168,7 @@ namespace QMatrixClient
template <> struct FromJson<QJsonArray>
{
- QJsonArray operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
return jv.toArray();
}
@@ -153,10 +176,11 @@ namespace QMatrixClient
template <typename T> struct FromJson<std::vector<T>>
{
- std::vector<T> operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
+ using size_type = typename std::vector<T>::size_type;
const auto jsonArray = jv.toArray();
- std::vector<T> vect; vect.resize(size_t(jsonArray.size()));
+ std::vector<T> vect; vect.resize(size_type(jsonArray.size()));
std::transform(jsonArray.begin(), jsonArray.end(),
vect.begin(), FromJson<T>());
return vect;
@@ -165,7 +189,7 @@ namespace QMatrixClient
template <typename T> struct FromJson<QVector<T>>
{
- QVector<T> operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
const auto jsonArray = jv.toArray();
QVector<T> vect; vect.resize(jsonArray.size());
@@ -177,7 +201,7 @@ namespace QMatrixClient
template <typename T> struct FromJson<QList<T>>
{
- QList<T> operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
const auto jsonArray = jv.toArray();
QList<T> sl; sl.reserve(jsonArray.size());
@@ -191,7 +215,7 @@ namespace QMatrixClient
template <> struct FromJson<QByteArray>
{
- inline QByteArray operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
return fromJson<QString>(jv).toLatin1();
}
@@ -199,7 +223,7 @@ namespace QMatrixClient
template <> struct FromJson<QVariantMap>
{
- inline auto operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
return jv.toObject().toVariantMap();
}
@@ -208,7 +232,7 @@ namespace QMatrixClient
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
template <> struct FromJson<QVariantHash>
{
- inline auto operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
return jv.toObject().toVariantHash();
}
@@ -217,7 +241,7 @@ namespace QMatrixClient
template <typename T> struct FromJson<QHash<QString, T>>
{
- QHash<QString, T> operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonValue& jv) const
{
const auto json = jv.toObject();
QHash<QString, T> h; h.reserve(json.size());
@@ -226,4 +250,16 @@ namespace QMatrixClient
return h;
}
};
+
+ template <typename T> struct FromJson<std::unordered_map<QString, T>>
+ {
+ auto operator()(const QJsonValue& jv) const
+ {
+ const auto json = jv.toObject();
+ std::unordered_map<QString, T> h; h.reserve(size_t(json.size()));
+ for (auto it = json.begin(); it != json.end(); ++it)
+ h.insert(std::make_pair(it.key(), fromJson<T>(it.value())));
+ return h;
+ }
+ };
} // namespace QMatrixClient