aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-08-01 12:11:58 +0200
committerAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-08-01 12:15:09 +0200
commit150df8ce08976ac7b8c25dbed1b965b7c2f65249 (patch)
tree4441d9c617251c8045af94abee59a47aa070dd59 /lib
parentd4b8f54f764bec5758c8f672d4ab05d59e02c269 (diff)
downloadlibquotient-150df8ce08976ac7b8c25dbed1b965b7c2f65249.tar.gz
libquotient-150df8ce08976ac7b8c25dbed1b965b7c2f65249.zip
Pull out common JsonConverter code to JsonObjectUnpacker
Diffstat (limited to 'lib')
-rw-r--r--lib/converters.h20
-rw-r--r--lib/events/eventloader.h11
2 files changed, 16 insertions, 15 deletions
diff --git a/lib/converters.h b/lib/converters.h
index bf456af9..c1d1e9f9 100644
--- a/lib/converters.h
+++ b/lib/converters.h
@@ -31,6 +31,17 @@ struct JsonObjectConverter {
template <typename PodT, typename JsonT>
PodT fromJson(const JsonT&);
+template <typename T>
+struct JsonObjectUnpacker {
+ // By default, revert to fromJson() so that one could provide a single
+ // fromJson<T, QJsonObject> specialisation instead of specialising
+ // the entire JsonConverter; if a different type of JSON value is needed
+ // (e.g., an array), specialising JsonConverter is inevitable
+ static T load(QJsonValueRef jvr) { return fromJson<T>(QJsonValue(jvr)); }
+ static T load(const QJsonValue& jv) { return fromJson<T>(jv.toObject()); }
+ static T load(const QJsonDocument& jd) { return fromJson<T>(jd.object()); }
+};
+
//! \brief The switchboard for extra conversion algorithms behind from/toJson
//!
//! This template is mainly intended for partial conversion specialisations
@@ -47,7 +58,7 @@ PodT fromJson(const JsonT&);
//! that they are not supported and it's not feasible to support those by means
//! of overloading toJson() and specialising fromJson().
template <typename T>
-struct JsonConverter {
+struct JsonConverter : JsonObjectUnpacker<T> {
// Unfortunately, if constexpr doesn't work with dump() and T::toJson
// because trying to check invocability of T::toJson hits a hard
// (non-SFINAE) compilation error if the member is not there. Hence a bit
@@ -77,13 +88,6 @@ struct JsonConverter {
return pod;
}
}
- // By default, revert to fromJson() so that one could provide a single
- // fromJson<T, QJsonObject> specialisation instead of specialising
- // the entire JsonConverter; if a different type of JSON value is needed
- // (e.g., an array), specialising JsonConverter is inevitable
- static T load(QJsonValueRef jvr) { return fromJson<T>(QJsonValue(jvr)); }
- static T load(const QJsonValue& jv) { return fromJson<T>(jv.toObject()); }
- static T load(const QJsonDocument& jd) { return fromJson<T>(jd.object()); }
};
template <typename T>
diff --git a/lib/events/eventloader.h b/lib/events/eventloader.h
index c7b82e8e..7dde9786 100644
--- a/lib/events/eventloader.h
+++ b/lib/events/eventloader.h
@@ -48,14 +48,11 @@ inline StateEventPtr loadStateEvent(const QString& matrixType,
}
template <typename EventT>
-struct JsonConverter<event_ptr_tt<EventT>> {
- static auto load(const QJsonValue& jv)
+struct JsonConverter<event_ptr_tt<EventT>>
+ : JsonObjectUnpacker<event_ptr_tt<EventT>> {
+ static auto load(const QJsonObject& jo)
{
- return loadEvent<EventT>(jv.toObject());
- }
- static auto load(const QJsonDocument& jd)
- {
- return loadEvent<EventT>(jd.object());
+ return loadEvent<EventT>(jo);
}
};
} // namespace Quotient