create encpsulator

This commit is contained in:
InsanusMokrassar 2021-01-21 10:10:52 +06:00
parent cce914091c
commit 14e666f18d
5 changed files with 36 additions and 0 deletions

View File

@ -2,6 +2,10 @@
## 0.4.20
* `Serialization`
* `Encapsulator`:
* Has been created
## 0.4.19
* `Coroutines`:

View File

@ -0,0 +1,7 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"

View File

@ -0,0 +1,23 @@
package dev.inmo.micro_utils.serialization.encapsulator
import kotlinx.serialization.KSerializer
import kotlinx.serialization.encoding.Encoder
import kotlin.reflect.KClass
data class Encapsulator<T : Any>(
val klass: KClass<T>,
val serializer: KSerializer<T>
) {
fun <O> encapsulate(
value: Any,
callback: KSerializer<T>.(T) -> O
): O? = if (klass.isInstance(value)) {
callback(serializer, value as T)
} else {
null
}
}
fun <T : Any> Encapsulator<T>.tryEncode(encoder: Encoder, value: Any) = encapsulate(value) {
encoder.encodeSerializableValue(this, it)
}

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.serialization.encapsulator"/>

View File

@ -22,6 +22,7 @@ String[] includes = [
":android:alerts:common",
":android:alerts:recyclerview",
":serialization:base64",
":serialization:encapsulator",
":dokka"
]