Skip to content

Commit 8e7569d

Browse files
authored
Fix T::class in parseJson causing type erasure in some cases (#2852)
1 parent 0728dd0 commit 8e7569d

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

  • app/src/main/java/com/lagradost/cloudstream3/utils
  • library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils

app/src/main/java/com/lagradost/cloudstream3/utils/InAppUpdater.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ object InAppUpdater {
103103
!rel.prerelease
104104
}.sortedWith(compareBy { release ->
105105
release.assets.firstOrNull { it.contentType == "application/vnd.android.package-archive" }?.name?.let { it1 ->
106-
versionRegex.find(
107-
it1
108-
)?.groupValues?.let {
106+
versionRegex.find(it1)?.groupValues?.let {
109107
it[3].toInt() * 100_000_000 + it[4].toInt() * 10_000 + it[5].toInt()
110108
}
111109
}

library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/AppUtils.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lagradost.cloudstream3.utils
22

3+
import com.fasterxml.jackson.core.type.TypeReference
34
import com.fasterxml.jackson.module.kotlin.readValue
45
import com.lagradost.cloudstream3.InternalAPI
56
import com.lagradost.cloudstream3.json
@@ -9,6 +10,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
910
import kotlinx.serialization.InternalSerializationApi
1011
import kotlinx.serialization.KSerializer
1112
import kotlinx.serialization.SerializationException
13+
import kotlinx.serialization.serializer
1214
import kotlinx.serialization.serializerOrNull
1315
import kotlin.reflect.KClass
1416

@@ -21,7 +23,10 @@ object AppUtils {
2123
}
2224

2325
inline fun <reified T : Any> parseJson(value: String): T {
24-
return parseJson(value, T::class)
26+
// serializer<T>() preserves full generic type info (e.g. List<DataClass>)
27+
// and must be resolved here while T is still reified, same for TypeReference
28+
val serializer = try { serializer<T>() } catch (_: Exception) { null }
29+
return parseJson(value, T::class, serializer, object : TypeReference<T>() {})
2530
}
2631

2732
@Deprecated(
@@ -63,17 +68,28 @@ object AppUtils {
6368
}
6469

6570
@InternalAPI
66-
fun <T : Any> parseJson(value: String, kClass: KClass<T>): T {
71+
fun <T : Any> parseJson(
72+
value: String,
73+
kClass: KClass<T>,
74+
serializer: KSerializer<T>? = null,
75+
typeReference: TypeReference<T>? = null,
76+
): T {
6777
// @Serializable generates a serializer at compile time; contextual serializers are
6878
// registered manually in serializersModule, we need both to support all cases
69-
val serializer = kClass.serializerOrNull() ?: json.serializersModule.getContextual(kClass)
70-
return if (serializer != null) {
79+
val s =
80+
serializer ?: kClass.serializerOrNull() ?: json.serializersModule.getContextual(kClass)
81+
82+
// Prefer Kotlin Serialization over Jackson
83+
if (s != null) {
7184
try {
72-
json.decodeFromString(serializer, value)
85+
return json.decodeFromString(s, value)
7386
} catch (e: SerializationException) {
7487
logError(e)
75-
mapper.readValue(value, kClass.java)
7688
}
89+
}
90+
91+
return if (typeReference != null) {
92+
mapper.readValue(value, typeReference)
7793
} else {
7894
mapper.readValue(value, kClass.java)
7995
}

0 commit comments

Comments
 (0)