diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-03-23 23:15:24 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-03-24 15:06:30 +0900 |
commit | 08fb435749f33ee5d266bbc2ea63c7c42169be97 (patch) | |
tree | 49376cb52cc2ff690752455d4884278a74f0927b | |
parent | a5428e53525afbc6fe6f697edb4f742d84c9ae0c (diff) | |
download | libquotient-08fb435749f33ee5d266bbc2ea63c7c42169be97.tar.gz libquotient-08fb435749f33ee5d266bbc2ea63c7c42169be97.zip |
converters.h: Make the default toJson() less greedy
template<T> toJson(T&&) grabbed even things that it cannot convert, leading to unpleasant effects in a situation when all that was needed was one implicit conversion (e.g. from QList<QString> to QStringList). So now it's three trivial toJson() overloads instead of a template.
-rw-r--r-- | converters.h | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/converters.h b/converters.h index 96efe5f8..f7d99f48 100644 --- a/converters.h +++ b/converters.h @@ -24,14 +24,13 @@ 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; } 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 +38,7 @@ namespace QMatrixClient return ar; } - inline QJsonValue toJson(const QStringList& strings) + inline QJsonArray toJson(const QStringList& strings) { return QJsonArray::fromStringList(strings); } @@ -50,7 +49,7 @@ namespace QMatrixClient } template <typename T> - inline QJsonValue toJson(const QHash<QString, T>& hashMap) + inline QJsonObject toJson(const QHash<QString, T>& hashMap) { QJsonObject json; for (auto it = hashMap.begin(); it != hashMap.end(); ++it) |