using json instance for decoding json element in custom serializers

This commit is contained in:
Adam Brown 2022-08-19 17:36:48 +01:00
parent a734984ebb
commit d463f7028b
3 changed files with 7 additions and 5 deletions

View File

@ -17,8 +17,8 @@ internal object ApiTimelineMessageContentDeserializer : KSerializer<ApiTimelineE
require(decoder is JsonDecoder)
val element = decoder.decodeJsonElement()
return when (element.jsonObject["msgtype"]?.jsonPrimitive?.content) {
"m.text" -> 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
}
}

View File

@ -17,8 +17,8 @@ internal object EncryptedContentDeserializer : KSerializer<ApiEncryptedContent>
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")
}

View File

@ -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) {