mirror of
https://github.com/InsanusMokrassar/SDI.git
synced 2024-11-23 10:38:52 +00:00
optionalContextual
This commit is contained in:
parent
f9d5a583dd
commit
99cc49d758
@ -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
|
||||
|
||||
|
@ -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)
|
Loading…
Reference in New Issue
Block a user