From 0d8f1c8560c5d65712d1656856ab33b6de0adb09 Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Wed, 8 Feb 2023 11:10:28 +0800 Subject: [PATCH] Tweak Response.parseAs() to utilize intrinsics (#9047) --- .../eu/kanade/tachiyomi/network/OkHttpExtensions.kt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt b/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt index aa7dbccc66..f651317dc9 100644 --- a/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt +++ b/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt @@ -2,8 +2,8 @@ package eu.kanade.tachiyomi.network import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.ExperimentalSerializationApi -import kotlinx.serialization.KSerializer import kotlinx.serialization.json.Json import kotlinx.serialization.json.okio.decodeFromBufferedSource import kotlinx.serialization.serializer @@ -21,8 +21,6 @@ import uy.kohesive.injekt.api.get import java.io.IOException import java.util.concurrent.atomic.AtomicBoolean import kotlin.coroutines.resumeWithException -import kotlin.reflect.KType -import kotlin.reflect.typeOf val jsonMime = "application/json; charset=utf-8".toMediaType() @@ -134,13 +132,11 @@ fun OkHttpClient.newCachelessCallWithProgress(request: Request, listener: Progre } inline fun Response.parseAs(): T { - return internalParseAs(typeOf(), this) + return decodeFromJsonResponse(serializer(), this) } -@Suppress("UNCHECKED_CAST") @OptIn(ExperimentalSerializationApi::class) -fun internalParseAs(type: KType, response: Response): T { - val deserializer = serializer(type) as KSerializer +fun decodeFromJsonResponse(deserializer: DeserializationStrategy, response: Response): T { return response.body.source().use { Injekt.get().decodeFromBufferedSource(deserializer, it) }