aboutsummaryrefslogtreecommitdiff
path: root/lib/converters.h
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-09-24 19:20:10 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-09-29 21:59:39 +0900
commit4244cee8d5e0f760cccd2b45ad587670573ef03c (patch)
tree4806108543f1402247e6cce1dba987a98d6fe83b /lib/converters.h
parentf5c2e47fa1ab84fdaffe03c30ba973d7dea5ac05 (diff)
downloadlibquotient-4244cee8d5e0f760cccd2b45ad587670573ef03c.tar.gz
libquotient-4244cee8d5e0f760cccd2b45ad587670573ef03c.zip
Prepare for CS API 0.4.0
This commit consists of two parts: upgrading the API infrastructure and trivial but sweeping update to the generated files. 1. The API infrastructure (converters.h, *.mustache and some other non-generated files) now can deal with top-level JSON arrays and response inlining; better supports property maps; and gets some formatting fixes in generated code. 2. Generated files now use QJsonValue instead of QJsonObject as a default type to (un)marshall Matrix API data structures, to match the change in the infrastructure above This commit is still using the old Matrix API definitions, before CS API 0.4.0. Getting to CS API 0.4.0 will come next.
Diffstat (limited to 'lib/converters.h')
-rw-r--r--lib/converters.h112
1 files changed, 67 insertions, 45 deletions
diff --git a/lib/converters.h b/lib/converters.h
index 7f78effe..70938ab9 100644
--- a/lib/converters.h
+++ b/lib/converters.h
@@ -22,6 +22,7 @@
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray> // Includes <QtCore/QJsonValue>
+#include <QtCore/QJsonDocument>
#include <QtCore/QDate>
#include <QtCore/QUrlQuery>
#include <QtCore/QSet>
@@ -80,7 +81,7 @@ namespace QMatrixClient
// non-explicit constructors.
QJsonValue variantToJson(const QVariant& v);
template <typename T>
- inline auto toJson(T&& var)
+ inline auto toJson(T&& /* const QVariant& or QVariant&& */ var)
-> std::enable_if_t<std::is_same<std::decay_t<T>, QVariant>::value,
QJsonValue>
{
@@ -137,17 +138,36 @@ namespace QMatrixClient
}
template <typename T>
+ struct FromJsonObject
+ {
+ T operator()(const QJsonObject& jo) const { return T(jo); }
+ };
+
+ template <typename T>
struct FromJson
{
- T operator()(const QJsonValue& jv) const { return T(jv); }
+ T operator()(const QJsonValue& jv) const
+ {
+ return FromJsonObject<T>()(jv.toObject());
+ }
+ T operator()(const QJsonDocument& jd) const
+ {
+ return FromJsonObject<T>()(jd.object());
+ }
};
template <typename T>
- inline T fromJson(const QJsonValue& jv)
+ inline auto fromJson(const QJsonValue& jv)
{
return FromJson<T>()(jv);
}
+ template <typename T>
+ inline auto fromJson(const QJsonDocument& jd)
+ {
+ return FromJson<T>()(jd);
+ }
+
template <> struct FromJson<bool>
{
auto operator()(const QJsonValue& jv) const { return jv.toBool(); }
@@ -194,14 +214,6 @@ namespace QMatrixClient
}
};
- template <> struct FromJson<QJsonObject>
- {
- auto operator()(const QJsonValue& jv) const
- {
- return jv.toObject();
- }
- };
-
template <> struct FromJson<QJsonArray>
{
auto operator()(const QJsonValue& jv) const
@@ -223,31 +235,35 @@ namespace QMatrixClient
QVariant operator()(const QJsonValue& jv) const;
};
- template <typename T> struct FromJson<std::vector<T>>
+ template <typename VectorT>
+ struct ArrayFromJson
{
- auto operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonArray& ja) const
{
- using size_type = typename std::vector<T>::size_type;
- const auto jsonArray = jv.toArray();
- std::vector<T> vect; vect.resize(size_type(jsonArray.size()));
- std::transform(jsonArray.begin(), jsonArray.end(),
- vect.begin(), FromJson<T>());
+ using size_type = typename VectorT::size_type;
+ VectorT vect; vect.resize(size_type(ja.size()));
+ std::transform(ja.begin(), ja.end(),
+ vect.begin(), FromJson<typename VectorT::value_type>());
return vect;
}
- };
-
- template <typename T> struct FromJson<QVector<T>>
- {
auto operator()(const QJsonValue& jv) const
{
- const auto jsonArray = jv.toArray();
- QVector<T> vect; vect.resize(jsonArray.size());
- std::transform(jsonArray.begin(), jsonArray.end(),
- vect.begin(), FromJson<T>());
- return vect;
+ return operator()(jv.toArray());
+ }
+ auto operator()(const QJsonDocument& jd) const
+ {
+ return operator()(jd.array());
}
};
+ template <typename T>
+ struct FromJson<std::vector<T>> : ArrayFromJson<std::vector<T>>
+ { };
+
+ template <typename T>
+ struct FromJson<QVector<T>> : ArrayFromJson<QVector<T>>
+ { };
+
template <typename T> struct FromJson<QList<T>>
{
auto operator()(const QJsonValue& jv) const
@@ -279,18 +295,36 @@ namespace QMatrixClient
}
};
- template <typename T> struct FromJson<QHash<QString, T>>
+ template <typename HashMapT>
+ struct HashMapFromJson
{
- auto operator()(const QJsonValue& jv) const
+ auto operator()(const QJsonObject& jo) const
{
- const auto json = jv.toObject();
- QHash<QString, T> h; h.reserve(json.size());
- for (auto it = json.begin(); it != json.end(); ++it)
- h.insert(it.key(), fromJson<T>(it.value()));
+ HashMapT h; h.reserve(jo.size());
+ for (auto it = jo.begin(); it != jo.end(); ++it)
+ h[it.key()] =
+ fromJson<typename HashMapT::mapped_type>(it.value());
return h;
}
+ auto operator()(const QJsonValue& jv) const
+ {
+ return operator()(jv.toObject());
+ }
+ auto operator()(const QJsonDocument& jd) const
+ {
+ return operator()(jd.object());
+ }
};
+ template <typename T>
+ struct FromJson<std::unordered_map<QString, T>>
+ : HashMapFromJson<std::unordered_map<QString, T>>
+ { };
+
+ template <typename T>
+ struct FromJson<QHash<QString, T>> : HashMapFromJson<QHash<QString, T>>
+ { };
+
#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
template <> struct FromJson<QHash<QString, QVariant>>
{
@@ -298,18 +332,6 @@ namespace QMatrixClient
};
#endif
- 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;
- }
- };
-
// Conditional insertion into a QJsonObject
namespace _impl