From 26bc529ec86dce5478ab37222a27902af7f0dd5a Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 13 Jul 2019 19:57:46 +0900 Subject: converters.h: fallback to intrusive toJson() for JsonObjectConverter; general improvements Single-argument fromJson>() now works as well. --- lib/converters.h | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/converters.h b/lib/converters.h index 5f00dc43..36b7ff15 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -60,8 +60,8 @@ namespace QMatrixClient template struct JsonObjectConverter { - static void dumpTo(QJsonObject& jo, const T& pod) { jo = pod; } - static void fillFrom(const QJsonObject& jo, T& pod) { pod = jo; } + static void dumpTo(QJsonObject& jo, const T& pod) { jo = pod.toJson(); } + static void fillFrom(const QJsonObject& jo, T& pod) { pod = T(jo); } }; template @@ -89,14 +89,16 @@ namespace QMatrixClient return JsonConverter::dump(pod); } + inline auto toJson(const QJsonObject& jo) { return jo; } + template - inline auto fillJson(QJsonObject& json, const T& data) + inline void fillJson(QJsonObject& json, const T& data) { JsonObjectConverter::dumpTo(json, data); } template - inline auto fromJson(const QJsonValue& jv) + inline T fromJson(const QJsonValue& jv) { return JsonConverter::load(jv); } @@ -114,8 +116,7 @@ namespace QMatrixClient template inline void fromJson(const QJsonValue& jv, T& pod) { - if (!jv.isUndefined()) - pod = fromJson(jv); + pod = jv.isUndefined() ? T() : fromJson(jv); } template @@ -124,21 +125,13 @@ namespace QMatrixClient pod = fromJson(jd); } - // Unfolds Omittable<> - template - inline void fromJson(const QJsonValue& jv, Omittable& pod) - { - if (jv.isUndefined()) - pod = none; - else - pod = fromJson(jv); - } - template inline void fillFromJson(const QJsonValue& jv, T& pod) { if (jv.isObject()) JsonObjectConverter::fillFrom(jv.toObject(), pod); + else if (!jv.isUndefined()) + pod = fromJson(jv); } // JsonConverter<> specialisations @@ -224,6 +217,21 @@ namespace QMatrixClient static QVariant load(const QJsonValue& jv); }; + template + struct JsonConverter> + { + static QJsonValue dump(const Omittable& from) + { + return from.omitted() ? QJsonValue() : toJson(from.value()); + } + static Omittable load(const QJsonValue& jv) + { + if (jv.isUndefined()) + return none; + return fromJson(jv); + } + }; + template struct JsonArrayConverter @@ -384,23 +392,20 @@ namespace QMatrixClient ForwardedT&& value) { if (!value.isEmpty()) - AddNode::impl(container, - key, std::forward(value)); + addTo(container, key, std::forward(value)); } }; - // This is a special one that unfolds Omittable<> - template - struct AddNode, Force> + // This one unfolds Omittable<> (also only when Force is false) + template + struct AddNode, false> { template static void impl(ContT& container, const QString& key, const OmittableT& value) { if (!value.omitted()) - AddNode::impl(container, key, value.value()); - else if (Force) // Edge case, no value but must put something - AddNode::impl(container, key, QString{}); + addTo(container, key, value.value()); } }; -- cgit v1.2.3