aboutsummaryrefslogtreecommitdiff
path: root/lib/application-service
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/application-service
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/application-service')
-rw-r--r--lib/application-service/definitions/location.cpp20
-rw-r--r--lib/application-service/definitions/location.h8
-rw-r--r--lib/application-service/definitions/protocol.cpp66
-rw-r--r--lib/application-service/definitions/protocol.h24
-rw-r--r--lib/application-service/definitions/user.cpp20
-rw-r--r--lib/application-service/definitions/user.h8
6 files changed, 52 insertions, 94 deletions
diff --git a/lib/application-service/definitions/location.cpp b/lib/application-service/definitions/location.cpp
index 958a55bf..a53db8d7 100644
--- a/lib/application-service/definitions/location.cpp
+++ b/lib/application-service/definitions/location.cpp
@@ -6,25 +6,19 @@
using namespace QMatrixClient;
-QJsonObject QMatrixClient::toJson(const ThirdPartyLocation& pod)
+void JsonObjectConverter<ThirdPartyLocation>::dumpTo(
+ QJsonObject& jo, const ThirdPartyLocation& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("alias"), pod.alias);
addParam<>(jo, QStringLiteral("protocol"), pod.protocol);
addParam<>(jo, QStringLiteral("fields"), pod.fields);
- return jo;
}
-ThirdPartyLocation FromJsonObject<ThirdPartyLocation>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<ThirdPartyLocation>::fillFrom(
+ const QJsonObject& jo, ThirdPartyLocation& result)
{
- ThirdPartyLocation result;
- result.alias =
- fromJson<QString>(jo.value("alias"_ls));
- result.protocol =
- fromJson<QString>(jo.value("protocol"_ls));
- result.fields =
- fromJson<QJsonObject>(jo.value("fields"_ls));
-
- return result;
+ fromJson(jo.value("alias"_ls), result.alias);
+ fromJson(jo.value("protocol"_ls), result.protocol);
+ fromJson(jo.value("fields"_ls), result.fields);
}
diff --git a/lib/application-service/definitions/location.h b/lib/application-service/definitions/location.h
index 89b48a43..5586cfc6 100644
--- a/lib/application-service/definitions/location.h
+++ b/lib/application-service/definitions/location.h
@@ -21,12 +21,10 @@ namespace QMatrixClient
/// Information used to identify this third party location.
QJsonObject fields;
};
-
- QJsonObject toJson(const ThirdPartyLocation& pod);
-
- template <> struct FromJsonObject<ThirdPartyLocation>
+ template <> struct JsonObjectConverter<ThirdPartyLocation>
{
- ThirdPartyLocation operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const ThirdPartyLocation& pod);
+ static void fillFrom(const QJsonObject& jo, ThirdPartyLocation& pod);
};
} // namespace QMatrixClient
diff --git a/lib/application-service/definitions/protocol.cpp b/lib/application-service/definitions/protocol.cpp
index 04bb7dfc..2a62b15d 100644
--- a/lib/application-service/definitions/protocol.cpp
+++ b/lib/application-service/definitions/protocol.cpp
@@ -6,75 +6,55 @@
using namespace QMatrixClient;
-QJsonObject QMatrixClient::toJson(const FieldType& pod)
+void JsonObjectConverter<FieldType>::dumpTo(
+ QJsonObject& jo, const FieldType& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("regexp"), pod.regexp);
addParam<>(jo, QStringLiteral("placeholder"), pod.placeholder);
- return jo;
}
-FieldType FromJsonObject<FieldType>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<FieldType>::fillFrom(
+ const QJsonObject& jo, FieldType& result)
{
- FieldType result;
- result.regexp =
- fromJson<QString>(jo.value("regexp"_ls));
- result.placeholder =
- fromJson<QString>(jo.value("placeholder"_ls));
-
- return result;
+ fromJson(jo.value("regexp"_ls), result.regexp);
+ fromJson(jo.value("placeholder"_ls), result.placeholder);
}
-QJsonObject QMatrixClient::toJson(const ProtocolInstance& pod)
+void JsonObjectConverter<ProtocolInstance>::dumpTo(
+ QJsonObject& jo, const ProtocolInstance& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("desc"), pod.desc);
addParam<IfNotEmpty>(jo, QStringLiteral("icon"), pod.icon);
addParam<>(jo, QStringLiteral("fields"), pod.fields);
addParam<>(jo, QStringLiteral("network_id"), pod.networkId);
- return jo;
}
-ProtocolInstance FromJsonObject<ProtocolInstance>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<ProtocolInstance>::fillFrom(
+ const QJsonObject& jo, ProtocolInstance& result)
{
- ProtocolInstance result;
- result.desc =
- fromJson<QString>(jo.value("desc"_ls));
- result.icon =
- fromJson<QString>(jo.value("icon"_ls));
- result.fields =
- fromJson<QJsonObject>(jo.value("fields"_ls));
- result.networkId =
- fromJson<QString>(jo.value("network_id"_ls));
-
- return result;
+ fromJson(jo.value("desc"_ls), result.desc);
+ fromJson(jo.value("icon"_ls), result.icon);
+ fromJson(jo.value("fields"_ls), result.fields);
+ fromJson(jo.value("network_id"_ls), result.networkId);
}
-QJsonObject QMatrixClient::toJson(const ThirdPartyProtocol& pod)
+void JsonObjectConverter<ThirdPartyProtocol>::dumpTo(
+ QJsonObject& jo, const ThirdPartyProtocol& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("user_fields"), pod.userFields);
addParam<>(jo, QStringLiteral("location_fields"), pod.locationFields);
addParam<>(jo, QStringLiteral("icon"), pod.icon);
addParam<>(jo, QStringLiteral("field_types"), pod.fieldTypes);
addParam<>(jo, QStringLiteral("instances"), pod.instances);
- return jo;
}
-ThirdPartyProtocol FromJsonObject<ThirdPartyProtocol>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<ThirdPartyProtocol>::fillFrom(
+ const QJsonObject& jo, ThirdPartyProtocol& result)
{
- ThirdPartyProtocol result;
- result.userFields =
- fromJson<QStringList>(jo.value("user_fields"_ls));
- result.locationFields =
- fromJson<QStringList>(jo.value("location_fields"_ls));
- result.icon =
- fromJson<QString>(jo.value("icon"_ls));
- result.fieldTypes =
- fromJson<QHash<QString, FieldType>>(jo.value("field_types"_ls));
- result.instances =
- fromJson<QVector<ProtocolInstance>>(jo.value("instances"_ls));
-
- return result;
+ fromJson(jo.value("user_fields"_ls), result.userFields);
+ fromJson(jo.value("location_fields"_ls), result.locationFields);
+ fromJson(jo.value("icon"_ls), result.icon);
+ fromJson(jo.value("field_types"_ls), result.fieldTypes);
+ fromJson(jo.value("instances"_ls), result.instances);
}
diff --git a/lib/application-service/definitions/protocol.h b/lib/application-service/definitions/protocol.h
index 2aca7d66..0a1f9a21 100644
--- a/lib/application-service/definitions/protocol.h
+++ b/lib/application-service/definitions/protocol.h
@@ -25,12 +25,10 @@ namespace QMatrixClient
/// An placeholder serving as a valid example of the field value.
QString placeholder;
};
-
- QJsonObject toJson(const FieldType& pod);
-
- template <> struct FromJsonObject<FieldType>
+ template <> struct JsonObjectConverter<FieldType>
{
- FieldType operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const FieldType& pod);
+ static void fillFrom(const QJsonObject& jo, FieldType& pod);
};
struct ProtocolInstance
@@ -45,12 +43,10 @@ namespace QMatrixClient
/// A unique identifier across all instances.
QString networkId;
};
-
- QJsonObject toJson(const ProtocolInstance& pod);
-
- template <> struct FromJsonObject<ProtocolInstance>
+ template <> struct JsonObjectConverter<ProtocolInstance>
{
- ProtocolInstance operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const ProtocolInstance& pod);
+ static void fillFrom(const QJsonObject& jo, ProtocolInstance& pod);
};
struct ThirdPartyProtocol
@@ -78,12 +74,10 @@ namespace QMatrixClient
/// same application service.
QVector<ProtocolInstance> instances;
};
-
- QJsonObject toJson(const ThirdPartyProtocol& pod);
-
- template <> struct FromJsonObject<ThirdPartyProtocol>
+ template <> struct JsonObjectConverter<ThirdPartyProtocol>
{
- ThirdPartyProtocol operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const ThirdPartyProtocol& pod);
+ static void fillFrom(const QJsonObject& jo, ThirdPartyProtocol& pod);
};
} // namespace QMatrixClient
diff --git a/lib/application-service/definitions/user.cpp b/lib/application-service/definitions/user.cpp
index ca334236..8ba92321 100644
--- a/lib/application-service/definitions/user.cpp
+++ b/lib/application-service/definitions/user.cpp
@@ -6,25 +6,19 @@
using namespace QMatrixClient;
-QJsonObject QMatrixClient::toJson(const ThirdPartyUser& pod)
+void JsonObjectConverter<ThirdPartyUser>::dumpTo(
+ QJsonObject& jo, const ThirdPartyUser& pod)
{
- QJsonObject jo;
addParam<>(jo, QStringLiteral("userid"), pod.userid);
addParam<>(jo, QStringLiteral("protocol"), pod.protocol);
addParam<>(jo, QStringLiteral("fields"), pod.fields);
- return jo;
}
-ThirdPartyUser FromJsonObject<ThirdPartyUser>::operator()(const QJsonObject& jo) const
+void JsonObjectConverter<ThirdPartyUser>::fillFrom(
+ const QJsonObject& jo, ThirdPartyUser& result)
{
- ThirdPartyUser result;
- result.userid =
- fromJson<QString>(jo.value("userid"_ls));
- result.protocol =
- fromJson<QString>(jo.value("protocol"_ls));
- result.fields =
- fromJson<QJsonObject>(jo.value("fields"_ls));
-
- return result;
+ fromJson(jo.value("userid"_ls), result.userid);
+ fromJson(jo.value("protocol"_ls), result.protocol);
+ fromJson(jo.value("fields"_ls), result.fields);
}
diff --git a/lib/application-service/definitions/user.h b/lib/application-service/definitions/user.h
index 79ca7789..062d2cac 100644
--- a/lib/application-service/definitions/user.h
+++ b/lib/application-service/definitions/user.h
@@ -21,12 +21,10 @@ namespace QMatrixClient
/// Information used to identify this third party location.
QJsonObject fields;
};
-
- QJsonObject toJson(const ThirdPartyUser& pod);
-
- template <> struct FromJsonObject<ThirdPartyUser>
+ template <> struct JsonObjectConverter<ThirdPartyUser>
{
- ThirdPartyUser operator()(const QJsonObject& jo) const;
+ static void dumpTo(QJsonObject& jo, const ThirdPartyUser& pod);
+ static void fillFrom(const QJsonObject& jo, ThirdPartyUser& pod);
};
} // namespace QMatrixClient