using json instance for decoding json element in custom serializers
This commit is contained in:
parent
a734984ebb
commit
d463f7028b
|
@ -17,8 +17,8 @@ internal object ApiTimelineMessageContentDeserializer : KSerializer<ApiTimelineE
|
||||||
require(decoder is JsonDecoder)
|
require(decoder is JsonDecoder)
|
||||||
val element = decoder.decodeJsonElement()
|
val element = decoder.decodeJsonElement()
|
||||||
return when (element.jsonObject["msgtype"]?.jsonPrimitive?.content) {
|
return when (element.jsonObject["msgtype"]?.jsonPrimitive?.content) {
|
||||||
"m.text" -> ApiTimelineEvent.TimelineMessage.Content.Text.serializer().deserialize(decoder)
|
"m.text" -> decoder.json.decodeFromJsonElement(ApiTimelineEvent.TimelineMessage.Content.Text.serializer(), element)
|
||||||
"m.image" -> ApiTimelineEvent.TimelineMessage.Content.Image.serializer().deserialize(decoder)
|
"m.image" -> decoder.json.decodeFromJsonElement(ApiTimelineEvent.TimelineMessage.Content.Image.serializer(), element)
|
||||||
else -> ApiTimelineEvent.TimelineMessage.Content.Ignored
|
else -> ApiTimelineEvent.TimelineMessage.Content.Ignored
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ internal object EncryptedContentDeserializer : KSerializer<ApiEncryptedContent>
|
||||||
require(decoder is JsonDecoder)
|
require(decoder is JsonDecoder)
|
||||||
val element = decoder.decodeJsonElement()
|
val element = decoder.decodeJsonElement()
|
||||||
return when (val algorithm = element.jsonObject["algorithm"]?.jsonPrimitive?.content) {
|
return when (val algorithm = element.jsonObject["algorithm"]?.jsonPrimitive?.content) {
|
||||||
"m.olm.v1.curve25519-aes-sha2" -> ApiEncryptedContent.OlmV1.serializer().deserialize(decoder)
|
"m.olm.v1.curve25519-aes-sha2" -> decoder.json.decodeFromJsonElement(ApiEncryptedContent.OlmV1.serializer(), element)
|
||||||
"m.megolm.v1.aes-sha2" -> ApiEncryptedContent.MegOlmV1.serializer().deserialize(decoder)
|
"m.megolm.v1.aes-sha2" -> decoder.json.decodeFromJsonElement(ApiEncryptedContent.MegOlmV1.serializer(), element)
|
||||||
null -> ApiEncryptedContent.Unknown
|
null -> ApiEncryptedContent.Unknown
|
||||||
else -> throw IllegalArgumentException("Unknown algorithm : $algorithm")
|
else -> throw IllegalArgumentException("Unknown algorithm : $algorithm")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_DECRYPTED_CONTENT = DecryptedContent.TimelineText(aTimelineTextEventContent(body = A_DECRYPTED_MESSAGE_CONTENT))
|
||||||
private val A_USER_CREDENTIALS = aUserCredentials()
|
private val A_USER_CREDENTIALS = aUserCredentials()
|
||||||
|
|
||||||
|
private val json = Json { encodeDefaults = true }
|
||||||
|
|
||||||
class RoomEventsDecrypterTest {
|
class RoomEventsDecrypterTest {
|
||||||
|
|
||||||
private val fakeMessageDecrypter = FakeMessageDecrypter()
|
private val fakeMessageDecrypter = FakeMessageDecrypter()
|
||||||
|
@ -67,7 +69,7 @@ class RoomEventsDecrypterTest {
|
||||||
private fun givenEncryptedMessage(roomMessage: RoomEvent.Message, decryptsTo: DecryptedContent) {
|
private fun givenEncryptedMessage(roomMessage: RoomEvent.Message, decryptsTo: DecryptedContent) {
|
||||||
val model = roomMessage.encryptedContent!!.toModel()
|
val model = roomMessage.encryptedContent!!.toModel()
|
||||||
fakeMessageDecrypter.givenDecrypt(model)
|
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) {
|
private fun givenEncryptedReply(roomReply: RoomEvent.Reply, decryptsTo: DecryptedContent) {
|
||||||
|
|
Loading…
Reference in New Issue