aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-04 18:54:52 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-04 20:24:24 +0900
commit54c45b52cec717baf0448b4928e606f518a5f8fe (patch)
tree20356c2a1b9f805b681b9b84e638e2bd7b9ad8dc /lib
parentb71f291d1979355c5efd7f53988d1d1acf294b09 (diff)
downloadlibquotient-54c45b52cec717baf0448b4928e606f518a5f8fe.tar.gz
libquotient-54c45b52cec717baf0448b4928e606f518a5f8fe.zip
converters.h: support std::unordered_map<QString, T>
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