Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.lagradost.cloudstream3.CloudStreamApp.Companion.removeKey
import com.lagradost.cloudstream3.CloudStreamApp.Companion.setKeyClass
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.AppUtils.parseJson
import com.lagradost.cloudstream3.utils.AppUtils.toJson
import com.lagradost.cloudstream3.utils.AppUtils.toJsonLiteral
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -173,7 +173,7 @@ object DataStore {
fun <T> Context.setKey(path: String, value: T) {
try {
getSharedPrefs().edit {
putString(path, value?.let { it.toJson() })
putString(path, value?.toJsonLiteral())
}
} catch (e: Exception) {
logError(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,10 @@ import kotlin.reflect.KClass

@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
object AppUtils {
/** Any object as json string */
/** Any object as a JSON string */
fun Any.toJson(): String {
if (this is String) return this
// @Serializable generates a serializer at compile time; contextual serializers are
// registered manually in serializersModule, we need both to support all cases
val serializer = this::class.serializerOrNull() ?: json.serializersModule.getContextual(this::class)
return if (serializer != null) {
try {
@Suppress("UNCHECKED_CAST")
json.encodeToString(serializer as KSerializer<Any>, this)
} catch (e: SerializationException) {
logError(e)
mapper.writeValueAsString(this)
}
} else {
mapper.writeValueAsString(this)
}
return toJsonLiteral()
}

inline fun <reified T : Any> parseJson(value: String): T {
Expand Down Expand Up @@ -68,6 +55,25 @@ object AppUtils {
}
}

/** Sometimes we want to encode as JSON even if it is already a String. */
@InternalAPI
fun Any.toJsonLiteral(): String {
// @Serializable generates a serializer at compile time; contextual serializers are
// registered manually in serializersModule, we need both to support all cases
val serializer = this::class.serializerOrNull() ?: json.serializersModule.getContextual(this::class)
return if (serializer != null) {
try {
@Suppress("UNCHECKED_CAST")
json.encodeToString(serializer as KSerializer<Any>, this)
} catch (e: SerializationException) {
logError(e)
mapper.writeValueAsString(this)
}
} else {
mapper.writeValueAsString(this)
}
}

@InternalAPI
fun <T : Any> parseJson(value: String, kClass: KClass<T>): T {
// @Serializable generates a serializer at compile time; contextual serializers are
Expand Down