aboutsummaryrefslogtreecommitdiff
path: root/lib/identity
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-12-08 15:36:04 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-12-08 20:12:22 +0900
commit3392e66fd015e191b01f6e3fc6839edc3948e31f (patch)
treec839259aece7462d978f7aa9eeb712edd932cc98 /lib/identity
parent95d4df58b39962f771885a6615efe1a682aab356 (diff)
downloadlibquotient-3392e66fd015e191b01f6e3fc6839edc3948e31f.tar.gz
libquotient-3392e66fd015e191b01f6e3fc6839edc3948e31f.zip
Refactor toJson/fillJson
Both now use through a common JsonConverter<> template class with its base definition tuned for structs/QJsonObjects and specialisations for non-object types. This new implementation doesn't work with virtual fillJson functions yet (so EventContent classes still use toJson as a member function) and does not cope quite well with non-constructible objects (you have to specialise JsonConverter<> rather than, more intuitively, JsonObjectConverter<>), but overall is more streamlined compared to the previous implementation. It also fixes one important issue that pushed for a rewrite: the previous implementation was not working with structure hierarchies at all so (in particular) the Filter part of CS API was totally disfunctional.
Diffstat (limited to 'lib/identity')
-rw-r--r--lib/identity/definitions/request_email_validation.cpp23
-rw-r--r--lib/identity/definitions/request_email_validation.h8
-rw-r--r--lib/identity/definitions/request_msisdn_validation.cpp26
-rw-r--r--lib/identity/definitions/request_msisdn_validation.h8
-rw-r--r--lib/identity/definitions/sid.cpp14
-rw-r--r--lib/identity/definitions/sid.h8
6 files changed, 31 insertions, 56 deletions
diff --git a/lib/identity/definitions/request_email_validation.cpp b/lib/identity/definitions/request_email_validation.cpp
index 95088bcb..47463a8b 100644
--- a/lib/identity/definitions/request_email_validation.cpp
+++ b/lib/identity/definitions/request_email_validation.cpp
@@ -6,28 +6,21 @@
using namespace QMatrixClient;
-QJsonObject QMatrixClient::toJson(const RequestEmailValidation& pod)
+void JsonObjectConverter<RequestEmailValidation>::dumpTo(
+ QJsonObject& jo, const RequestEmailValidation& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("client_secret"), pod.clientSecret);
addParam<>(jo, QStringLiteral("email"), pod.email);
addParam<>(jo, QStringLiteral("send_attempt"), pod.sendAttempt);
addParam<IfNotEmpty>(jo, QStringLiteral("next_link"), pod.nextLink);
- return jo;
}
-RequestEmailValidation FromJsonObject<RequestEmailValidation>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<RequestEmailValidation>::fillFrom(
+ const QJsonObject& jo, RequestEmailValidation& result)
{
- RequestEmailValidation result;
- result.clientSecret =
- fromJson<QString>(jo.value("client_secret"_ls));
- result.email =
- fromJson<QString>(jo.value("email"_ls));
- result.sendAttempt =
- fromJson<int>(jo.value("send_attempt"_ls));
- result.nextLink =
- fromJson<QString>(jo.value("next_link"_ls));
-
- return result;
+ fromJson(jo.value("client_secret"_ls), result.clientSecret);
+ fromJson(jo.value("email"_ls), result.email);
+ fromJson(jo.value("send_attempt"_ls), result.sendAttempt);
+ fromJson(jo.value("next_link"_ls), result.nextLink);
}
diff --git a/lib/identity/definitions/request_email_validation.h b/lib/identity/definitions/request_email_validation.h
index 3e72275f..eb7d8ed6 100644
--- a/lib/identity/definitions/request_email_validation.h
+++ b/lib/identity/definitions/request_email_validation.h
@@ -33,12 +33,10 @@ namespace QMatrixClient
/// server will redirect the user to this URL.
QString nextLink;
};
-
- QJsonObject toJson(const RequestEmailValidation& pod);
-
- template <> struct FromJsonObject<RequestEmailValidation>
+ template <> struct JsonObjectConverter<RequestEmailValidation>
{
- RequestEmailValidation operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const RequestEmailValidation& pod);
+ static void fillFrom(const QJsonObject& jo, RequestEmailValidation& pod);
};
} // namespace QMatrixClient
diff --git a/lib/identity/definitions/request_msisdn_validation.cpp b/lib/identity/definitions/request_msisdn_validation.cpp
index 125baa9c..a123d326 100644
--- a/lib/identity/definitions/request_msisdn_validation.cpp
+++ b/lib/identity/definitions/request_msisdn_validation.cpp
@@ -6,31 +6,23 @@
using namespace QMatrixClient;
-QJsonObject QMatrixClient::toJson(const RequestMsisdnValidation& pod)
+void JsonObjectConverter<RequestMsisdnValidation>::dumpTo(
+ QJsonObject& jo, const RequestMsisdnValidation& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("client_secret"), pod.clientSecret);
addParam<>(jo, QStringLiteral("country"), pod.country);
addParam<>(jo, QStringLiteral("phone_number"), pod.phoneNumber);
addParam<>(jo, QStringLiteral("send_attempt"), pod.sendAttempt);
addParam<IfNotEmpty>(jo, QStringLiteral("next_link"), pod.nextLink);
- return jo;
}
-RequestMsisdnValidation FromJsonObject<RequestMsisdnValidation>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<RequestMsisdnValidation>::fillFrom(
+ const QJsonObject& jo, RequestMsisdnValidation& result)
{
- RequestMsisdnValidation result;
- result.clientSecret =
- fromJson<QString>(jo.value("client_secret"_ls));
- result.country =
- fromJson<QString>(jo.value("country"_ls));
- result.phoneNumber =
- fromJson<QString>(jo.value("phone_number"_ls));
- result.sendAttempt =
- fromJson<int>(jo.value("send_attempt"_ls));
- result.nextLink =
- fromJson<QString>(jo.value("next_link"_ls));
-
- return result;
+ fromJson(jo.value("client_secret"_ls), result.clientSecret);
+ fromJson(jo.value("country"_ls), result.country);
+ fromJson(jo.value("phone_number"_ls), result.phoneNumber);
+ fromJson(jo.value("send_attempt"_ls), result.sendAttempt);
+ fromJson(jo.value("next_link"_ls), result.nextLink);
}
diff --git a/lib/identity/definitions/request_msisdn_validation.h b/lib/identity/definitions/request_msisdn_validation.h
index 77bea2bc..b48ed6d5 100644
--- a/lib/identity/definitions/request_msisdn_validation.h
+++ b/lib/identity/definitions/request_msisdn_validation.h
@@ -36,12 +36,10 @@ namespace QMatrixClient
/// server will redirect the user to this URL.
QString nextLink;
};
-
- QJsonObject toJson(const RequestMsisdnValidation& pod);
-
- template <> struct FromJsonObject<RequestMsisdnValidation>
+ template <> struct JsonObjectConverter<RequestMsisdnValidation>
{
- RequestMsisdnValidation operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const RequestMsisdnValidation& pod);
+ static void fillFrom(const QJsonObject& jo, RequestMsisdnValidation& pod);
};
} // namespace QMatrixClient
diff --git a/lib/identity/definitions/sid.cpp b/lib/identity/definitions/sid.cpp
index 443dbedf..1ba4b3b5 100644
--- a/lib/identity/definitions/sid.cpp
+++ b/lib/identity/definitions/sid.cpp
@@ -6,19 +6,15 @@
using namespace QMatrixClient;
-QJsonObject QMatrixClient::toJson(const Sid& pod)
+void JsonObjectConverter<Sid>::dumpTo(
+ QJsonObject& jo, const Sid& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("sid"), pod.sid);
- return jo;
}
-Sid FromJsonObject<Sid>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<Sid>::fillFrom(
+ const QJsonObject& jo, Sid& result)
{
- Sid result;
- result.sid =
- fromJson<QString>(jo.value("sid"_ls));
-
- return result;
+ fromJson(jo.value("sid"_ls), result.sid);
}
diff --git a/lib/identity/definitions/sid.h b/lib/identity/definitions/sid.h
index eae60c47..ac8c4130 100644
--- a/lib/identity/definitions/sid.h
+++ b/lib/identity/definitions/sid.h
@@ -19,12 +19,10 @@ namespace QMatrixClient
/// must not be empty.
QString sid;
};
-
- QJsonObject toJson(const Sid& pod);
-
- template <> struct FromJsonObject<Sid>
+ template <> struct JsonObjectConverter<Sid>
{
- Sid operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const Sid& pod);
+ static void fillFrom(const QJsonObject& jo, Sid& pod);
};
} // namespace QMatrixClient