Merge pull request #41 from InsanusMokrassar/0.4.20

0.4.20
This commit is contained in:
InsanusMokrassar 2021-01-21 10:24:58 +06:00 committed by GitHub
commit a67bef6853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 2 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## 0.4.20
* `Serialization`
* `Encapsulator`:
* Has been created
## 0.4.19
* `Coroutines`:

View File

@ -40,5 +40,5 @@ dokka_version=1.4.20
# Project data
group=dev.inmo
version=0.4.19
android_code_version=23
version=0.4.20
android_code_version=24

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"
]