mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-23 09:07:14 +00:00
commit
a67bef6853
@ -1,5 +1,11 @@
|
||||
# Changelog
|
||||
|
||||
## 0.4.20
|
||||
|
||||
* `Serialization`
|
||||
* `Encapsulator`:
|
||||
* Has been created
|
||||
|
||||
## 0.4.19
|
||||
|
||||
* `Coroutines`:
|
||||
|
@ -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
|
||||
|
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