optionalContextual

This commit is contained in:
InsanusMokrassar 2020-08-12 13:58:00 +06:00
parent f9d5a583dd
commit 99cc49d758
2 changed files with 22 additions and 0 deletions

View File

@ -12,6 +12,7 @@
### 0.3.1
* `Kotlin`: `1.3.71` -> `1.3.72`
* Add `SerializersModuleBuilder#optionalContextual` for more comfortable usage of `contextual`
## 0.2.0

View File

@ -0,0 +1,21 @@
package com.insanusmokrassar.sdi.utils
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.modules.*
import kotlin.reflect.KClass
@InternalSerializationApi
fun <T : Any> SerializersModuleBuilder.optionalContextual(
kClass: KClass<T>, kSerializer: KSerializer<T>
) = try {
contextual(kClass, kSerializer)
true
} catch (e: SerializerAlreadyRegisteredException) {
false
}
@InternalSerializationApi
inline fun <reified T : Any> SerializersModuleBuilder.optionalContextual(
kSerializer: KSerializer<T>
) = optionalContextual(T::class, kSerializer)