From d463f7028ba0496bfe891db665e861f85c333613 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 19 Aug 2022 17:36:48 +0100 Subject: [PATCH] using json instance for decoding json element in custom serializers --- .../internal/request/ApiTimelineMessageContentDeserializer.kt | 4 ++-- .../sync/internal/request/EncryptedContentDeserializer.kt | 4 ++-- .../st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt index 4fd1604..324e73e 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt @@ -17,8 +17,8 @@ internal object ApiTimelineMessageContentDeserializer : KSerializer ApiTimelineEvent.TimelineMessage.Content.Text.serializer().deserialize(decoder) - "m.image" -> ApiTimelineEvent.TimelineMessage.Content.Image.serializer().deserialize(decoder) + "m.text" -> decoder.json.decodeFromJsonElement(ApiTimelineEvent.TimelineMessage.Content.Text.serializer(), element) + "m.image" -> decoder.json.decodeFromJsonElement(ApiTimelineEvent.TimelineMessage.Content.Image.serializer(), element) else -> ApiTimelineEvent.TimelineMessage.Content.Ignored } } diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/EncryptedContentDeserializer.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/EncryptedContentDeserializer.kt index 7fb6ba8..cd3c529 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/EncryptedContentDeserializer.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/EncryptedContentDeserializer.kt @@ -17,8 +17,8 @@ internal object EncryptedContentDeserializer : KSerializer require(decoder is JsonDecoder) val element = decoder.decodeJsonElement() return when (val algorithm = element.jsonObject["algorithm"]?.jsonPrimitive?.content) { - "m.olm.v1.curve25519-aes-sha2" -> ApiEncryptedContent.OlmV1.serializer().deserialize(decoder) - "m.megolm.v1.aes-sha2" -> ApiEncryptedContent.MegOlmV1.serializer().deserialize(decoder) + "m.olm.v1.curve25519-aes-sha2" -> decoder.json.decodeFromJsonElement(ApiEncryptedContent.OlmV1.serializer(), element) + "m.megolm.v1.aes-sha2" -> decoder.json.decodeFromJsonElement(ApiEncryptedContent.MegOlmV1.serializer(), element) null -> ApiEncryptedContent.Unknown else -> throw IllegalArgumentException("Unknown algorithm : $algorithm") } diff --git a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt index a366b48..3178ae9 100644 --- a/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt +++ b/matrix/services/sync/src/test/kotlin/app/dapk/st/matrix/sync/internal/room/RoomEventsDecrypterTest.kt @@ -23,6 +23,8 @@ private val AN_ENCRYPTED_ROOM_REPLY = aRoomReplyMessageEvent( private val A_DECRYPTED_CONTENT = DecryptedContent.TimelineText(aTimelineTextEventContent(body = A_DECRYPTED_MESSAGE_CONTENT)) private val A_USER_CREDENTIALS = aUserCredentials() +private val json = Json { encodeDefaults = true } + class RoomEventsDecrypterTest { private val fakeMessageDecrypter = FakeMessageDecrypter() @@ -67,7 +69,7 @@ class RoomEventsDecrypterTest { private fun givenEncryptedMessage(roomMessage: RoomEvent.Message, decryptsTo: DecryptedContent) { val model = roomMessage.encryptedContent!!.toModel() fakeMessageDecrypter.givenDecrypt(model) - .returns(aDecryptionSuccessResult(payload = JsonString(Json { encodeDefaults = true }.encodeToString(DecryptedContent.serializer(), decryptsTo)))) + .returns(aDecryptionSuccessResult(payload = JsonString(json.encodeToString(DecryptedContent.serializer(), decryptsTo)))) } private fun givenEncryptedReply(roomReply: RoomEvent.Reply, decryptsTo: DecryptedContent) {