From 80588a782ce702384802e3e0cb469f71e5640ef4 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 6 Dec 2017 21:21:27 +0900 Subject: Make BaseJob::Data consume QByteArray as well, not only QJsonObject This is needed to support cases of content-repo, where the request/response bodies are not JSON. --- jobs/basejob.h | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'jobs/basejob.h') diff --git a/jobs/basejob.h b/jobs/basejob.h index f8b367c6..66812774 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -69,29 +70,31 @@ namespace QMatrixClient } }; /** - * A simple wrapper around QJsonObject that represents a JSON data - * section of an HTTP request to a Matrix server. Facilitates - * creation from a list of key-value string pairs and dumping of - * a resulting JSON to a QByteArray. + * A simple wrapper that represents the request body. + * Provides a unified interface to dump an unstructured byte stream + * as well as JSON (and possibly other structures in the future) to + * a QByteArray consumed by QNetworkAccessManager request methods. */ - class Data : public QJsonObject + class Data { public: - using QJsonObject::QJsonObject; Data() = default; - explicit Data(const QJsonObject& o) : QJsonObject(o) { } -#if (QT_VERSION < QT_VERSION_CHECK(5, 4, 0)) - // This method exists in QJsonObject of newer Qt versions - Data(const std::initializer_list< QPair >& l) + Data(const QByteArray& a) : _payload(a) { } + Data(const QJsonObject& jo) + : _payload(fromJson(QJsonDocument(jo))) { } + Data(const QJsonArray& ja) + : _payload(fromJson(QJsonDocument(ja))) { } + QByteArray serialize() const { - for (auto i: l) - insert(i.first, i.second); + return _payload; } -#endif - QByteArray serialize() const + + private: + static QByteArray fromJson(const QJsonDocument& jd) { - return QJsonDocument(*this).toJson(); + return jd.toJson(QJsonDocument::Compact); } + QByteArray _payload; }; /** -- cgit v1.2.3