aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/connection.cpp2
-rw-r--r--lib/converters.cpp (renamed from lib/variant_converters.h)55
-rw-r--r--lib/converters.h70
-rw-r--r--lib/csapi/gtad.yaml9
-rw-r--r--lib/room.cpp2
5 files changed, 84 insertions, 54 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index dae7b663..8adb18b5 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -23,6 +23,7 @@
#include "events/directchatevent.h"
#include "room.h"
#include "settings.h"
+#include "converters.h"
#include "csapi/login.h"
#include "csapi/logout.h"
#include "csapi/receipts.h"
@@ -33,7 +34,6 @@
#include "jobs/syncjob.h"
#include "jobs/mediathumbnailjob.h"
#include "jobs/downloadfilejob.h"
-#include "variant_converters.h"
#include <QtNetwork/QDnsLookup>
#include <QtCore/QFile>
diff --git a/lib/variant_converters.h b/lib/converters.cpp
index 76c395e1..f726db4c 100644
--- a/lib/variant_converters.h
+++ b/lib/converters.cpp
@@ -16,41 +16,44 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#pragma once
+#include "converters.h"
#include <QtCore/QVariant>
-#include "converters.h"
+using namespace QMatrixClient;
+
+QJsonValue toJson(const QVariant& v)
+{
+ return QJsonValue::fromVariant(v);
+}
-namespace QMatrixClient
+QJsonObject toJson(const QVariantMap& map)
{
- inline QJsonObject toJson(const QVariantMap& map)
- {
- return QJsonObject::fromVariantMap(map);
- }
+ return QJsonObject::fromVariantMap(map);
+}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
- inline QJsonObject toJson(const QVariantHash& hMap)
- {
- return QJsonObject::fromVariantHash(hMap);
- }
+QJsonObject toJson(const QVariantHash& hMap)
+{
+ return QJsonObject::fromVariantHash(hMap);
+}
#endif
- template <> struct FromJson<QVariantMap>
- {
- auto operator()(const QJsonValue& jv) const
- {
- return jv.toObject().toVariantMap();
- }
- };
+QVariant FromJson<QVariant>::operator()(const QJsonValue& jv) const
+{
+ return jv.toVariant();
+}
+
+QMap<QString, QVariant>
+FromJson<QMap<QString, QVariant>>::operator()(const QJsonValue& jv) const
+{
+ return jv.toObject().toVariantMap();
+}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
- template <> struct FromJson<QVariantHash>
- {
- auto operator()(const QJsonValue& jv) const
- {
- return jv.toObject().toVariantHash();
- }
- };
-#endif
+QHash<QString, QVariant>
+FromJson<QHash<QString, QVariant>>::operator()(const QJsonValue& jv) const
+{
+ return jv.toObject().toVariantHash();
}
+#endif
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;
diff --git a/lib/csapi/gtad.yaml b/lib/csapi/gtad.yaml
index e3afd473..fada9c83 100644
--- a/lib/csapi/gtad.yaml
+++ b/lib/csapi/gtad.yaml
@@ -50,7 +50,7 @@ analyzer:
type: QDateTime
initializer: QDateTime::fromString("{{defaultValue}}")
imports: <QtCore/QDateTime>
- - //:
+ - //: &QString
type: QString
initializer: QStringLiteral("{{defaultValue}}")
isString:
@@ -68,7 +68,7 @@ analyzer:
- /event.yaml$/:
{ type: EventPtr, imports: '"events/event.h"' }
- /m\.room\.member$/: pass # This $ref is only used in an array, see below
- - //: # Apply "avoidCopy" to all other ref'ed types
+ - //: *UseOmittable # Also apply "avoidCopy" to all other ref'ed types
- array:
- string: { type: QStringList, imports: <QtCore/QStringList> }
- +set: { moveOnly: }
@@ -96,9 +96,10 @@ analyzer:
imports: <QtCore/QHash>
- //:
type: QVariantHash
- imports: '"variant_converters.h"'
+ imports: <QtCore/QVariant>
- variant: # A sequence `type` (multitype) in OpenAPI
- { type: QVariant, imports: '"variant_converters.h"' }
+ - "string|null": *QString
+ - //: { type: QVariant, imports: <QtCore/QVariant> }
- schema: # Properties of inline structure definitions
*UseOmittable
diff --git a/lib/room.cpp b/lib/room.cpp
index 9bcf0704..63b5e76d 100644
--- a/lib/room.cpp
+++ b/lib/room.cpp
@@ -40,7 +40,7 @@
#include "avatar.h"
#include "connection.h"
#include "user.h"
-#include "variant_converters.h"
+#include "converters.h"
#include <QtCore/QHash>
#include <QtCore/QStringBuilder> // for efficient string concats (operator%)