mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
create encpsulator
This commit is contained in:
parent
cce914091c
commit
14e666f18d
@ -2,6 +2,10 @@
|
||||
|
||||
## 0.4.20
|
||||
|
||||
* `Serialization`
|
||||
* `Encapsulator`:
|
||||
* Has been created
|
||||
|
||||
## 0.4.19
|
||||
|
||||
* `Coroutines`:
|
||||
|
7
serialization/encapsulator/build.gradle
Normal file
7
serialization/encapsulator/build.gradle
Normal file
@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
@ -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)
|
||||
}
|
1
serialization/encapsulator/src/main/AndroidManifest.xml
Normal file
1
serialization/encapsulator/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.micro_utils.serialization.encapsulator"/>
|
@ -22,6 +22,7 @@ String[] includes = [
|
||||
":android:alerts:common",
|
||||
":android:alerts:recyclerview",
|
||||
":serialization:base64",
|
||||
":serialization:encapsulator",
|
||||
|
||||
":dokka"
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user