aboutsummaryrefslogtreecommitdiff
path: root/lib/converters.h
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-06-03 17:12:35 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-06-03 17:12:35 +0900
commit38440a724acf8836e74e5bca29105e493f36eb7c (patch)
tree6c05bb4b413ee5ab72716a77b1128d2f2f8e3b38 /lib/converters.h
parent8a8c645f2a2dc91e9d956b7dee8ef5cf741541f4 (diff)
downloadlibquotient-38440a724acf8836e74e5bca29105e493f36eb7c.tar.gz
libquotient-38440a724acf8836e74e5bca29105e493f36eb7c.zip
csapi + converters: Support variant types (using QVariant)
This mandated some rearrangement of toJson() overloads and FromJson<> specializations for QVariant* types - instead of variant_converters.h they are now in converters.cpp.
Diffstat (limited to 'lib/converters.h')
-rw-r--r--lib/converters.h70
1 files changed, 48 insertions, 22 deletions
diff --git a/lib/converters.h b/lib/converters.h
index 87d5ad7e..ff06e232 100644
--- a/lib/converters.h
+++ b/lib/converters.h
@@ -43,6 +43,8 @@ namespace std
};
}
+class QVariant;
+
namespace QMatrixClient
{
struct NoneTag {};
@@ -87,12 +89,29 @@ namespace QMatrixClient
// 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; }
- // Special-case QStrings so that we could use isEmpty() on them
- // (see _impl::AddNote<> below)
- inline QString toJson(const QString& s) { return s; }
+ inline auto toJson(const QJsonValue& val) { return val; }
+ inline auto toJson(const QJsonObject& o) { return o; }
+ inline auto toJson(const QJsonArray& arr) { return arr; }
+ // Special-case QStrings and bools to avoid ambiguity between QJsonValue
+ // and QVariant (also, QString.isEmpty() is used in _impl::AddNote<> below)
+ inline auto toJson(const QString& s) { return s; }
+ inline QJsonValue toJson(bool b) { return b; }
+
+ inline QJsonArray toJson(const QStringList& strings)
+ {
+ return QJsonArray::fromStringList(strings);
+ }
+
+ inline QString toJson(const QByteArray& bytes)
+ {
+ return bytes.constData();
+ }
+
+ QJsonValue toJson(const QVariant& v);
+ QJsonObject toJson(const QMap<QString, QVariant>& map);
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
+ QJsonObject toJson(const QHash<QString, QVariant>& hMap);
+#endif
template <typename T>
inline QJsonArray toJson(const std::vector<T>& vals)
@@ -112,16 +131,6 @@ namespace QMatrixClient
return ar;
}
- inline QJsonArray toJson(const QStringList& strings)
- {
- return QJsonArray::fromStringList(strings);
- }
-
- inline QString toJson(const QByteArray& bytes)
- {
- return bytes.constData();
- }
-
template <typename T>
inline QJsonObject toJson(const QHash<QString, T>& hashMap)
{
@@ -230,6 +239,19 @@ namespace QMatrixClient
}
};
+ template <> struct FromJson<QByteArray>
+ {
+ auto operator()(const QJsonValue& jv) const
+ {
+ return fromJson<QString>(jv).toLatin1();
+ }
+ };
+
+ template <> struct FromJson<QVariant>
+ {
+ QVariant operator()(const QJsonValue& jv) const;
+ };
+
template <typename T> struct FromJson<std::vector<T>>
{
auto operator()(const QJsonValue& jv) const
@@ -269,12 +291,9 @@ namespace QMatrixClient
template <> struct FromJson<QStringList> : FromJson<QList<QString>> { };
- template <> struct FromJson<QByteArray>
+ template <> struct FromJson<QMap<QString, QVariant>>
{
- auto operator()(const QJsonValue& jv) const
- {
- return fromJson<QString>(jv).toLatin1();
- }
+ QMap<QString, QVariant> operator()(const QJsonValue& jv) const;
};
template <typename T> struct FromJson<QHash<QString, T>>
@@ -289,6 +308,13 @@ namespace QMatrixClient
}
};
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
+ template <> struct FromJson<QHash<QString, QVariant>>
+ {
+ QHash<QString, QVariant> operator()(const QJsonValue& jv) const;
+ };
+#endif
+
template <typename T> struct FromJson<std::unordered_map<QString, T>>
{
auto operator()(const QJsonValue& jv) const
@@ -325,7 +351,7 @@ namespace QMatrixClient
o.insert(std::move(key), std::forward<JsonT>(value));
}
};
- }
+ } // namespace _impl
static constexpr bool IfNotEmpty = false;