From 54c45b52cec717baf0448b4928e606f518a5f8fe Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 4 May 2018 18:54:52 +0900 Subject: converters.h: support std::unordered_map --- lib/converters.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/converters.h') 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 #include +#include + +// Enable std::unordered_map +namespace std +{ + template <> struct hash + { + 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 @@ -82,6 +96,15 @@ namespace QMatrixClient return json; } + template + inline QJsonObject toJson(const std::unordered_map& hashMap) + { + QJsonObject json; + for (auto it = hashMap.begin(); it != hashMap.end(); ++it) + json.insert(it.key(), toJson(it.value())); + return json; + } + template struct FromJson { @@ -227,4 +250,16 @@ namespace QMatrixClient return h; } }; + + template struct FromJson> + { + auto operator()(const QJsonValue& jv) const + { + const auto json = jv.toObject(); + std::unordered_map h; h.reserve(size_t(json.size())); + for (auto it = json.begin(); it != json.end(); ++it) + h.insert(std::make_pair(it.key(), fromJson(it.value()))); + return h; + } + }; } // namespace QMatrixClient -- cgit v1.2.3