diff options
-rw-r--r-- | lib/converters.h | 20 | ||||
-rw-r--r-- | lib/events/eventloader.h | 11 |
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 |