diff --git a/CHANGELOG.md b/CHANGELOG.md index 4183e078748..74476c33e82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.4.20 + +* `Serialization` + * `Encapsulator`: + * Has been created + ## 0.4.19 * `Coroutines`: diff --git a/gradle.properties b/gradle.properties index 728d265e171..cec7824686e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/serialization/encapsulator/build.gradle b/serialization/encapsulator/build.gradle new file mode 100644 index 00000000000..7c54502f100 --- /dev/null +++ b/serialization/encapsulator/build.gradle @@ -0,0 +1,7 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" + id "com.android.library" +} + +apply from: "$mppProjectWithSerializationPresetPath" diff --git a/serialization/encapsulator/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/encapsulator/Encapsulator.kt b/serialization/encapsulator/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/encapsulator/Encapsulator.kt new file mode 100644 index 00000000000..bb9cdca393a --- /dev/null +++ b/serialization/encapsulator/src/commonMain/kotlin/dev/inmo/micro_utils/serialization/encapsulator/Encapsulator.kt @@ -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( + val klass: KClass, + val serializer: KSerializer +) { + fun encapsulate( + value: Any, + callback: KSerializer.(T) -> O + ): O? = if (klass.isInstance(value)) { + callback(serializer, value as T) + } else { + null + } +} + +fun Encapsulator.tryEncode(encoder: Encoder, value: Any) = encapsulate(value) { + encoder.encodeSerializableValue(this, it) +} diff --git a/serialization/encapsulator/src/main/AndroidManifest.xml b/serialization/encapsulator/src/main/AndroidManifest.xml new file mode 100644 index 00000000000..eed58f5aa5d --- /dev/null +++ b/serialization/encapsulator/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index e53c506e681..6034af3c57d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -22,6 +22,7 @@ String[] includes = [ ":android:alerts:common", ":android:alerts:recyclerview", ":serialization:base64", + ":serialization:encapsulator", ":dokka" ]