aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/converters.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/converters.h b/lib/converters.h
index baa14c7b..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); }
@@ -227,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