From f5f75117810bbeab9ee4a9e79d93545814f5d77b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 9 Mar 2023 21:55:07 +0600 Subject: [PATCH] add mapper serializer --- CHANGELOG.md | 4 ++++ serialization/mapper/build.gradle | 7 ++++++ .../src/commonMain/kotlin/MapperSerializer.kt | 22 +++++++++++++++++++ settings.gradle | 1 + 4 files changed, 34 insertions(+) create mode 100644 serialization/mapper/build.gradle create mode 100644 serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 50220cc2871..a80f27c4420 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.17.4 +* `Serialization`: + * `Mapper`: + * Module inited + ## 0.17.3 * `Common`: diff --git a/serialization/mapper/build.gradle b/serialization/mapper/build.gradle new file mode 100644 index 00000000000..7c54502f100 --- /dev/null +++ b/serialization/mapper/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/mapper/src/commonMain/kotlin/MapperSerializer.kt b/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt new file mode 100644 index 00000000000..c18343deb82 --- /dev/null +++ b/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt @@ -0,0 +1,22 @@ +package dev.inmo.micro_utils.serialization.mapper + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +class MapperSerializer( + private val base: KSerializer, + private val serialize: (O) -> I, + private val deserialize: (I) -> O +) : KSerializer { + override val descriptor: SerialDescriptor = base.descriptor + + override fun deserialize(decoder: Decoder): O { + return deserialize(base.deserialize(decoder)) + } + + override fun serialize(encoder: Encoder, value: O) { + base.serialize(encoder, serialize(value)) + } +} diff --git a/settings.gradle b/settings.gradle index 6d422fe4e63..ec21091c550 100644 --- a/settings.gradle +++ b/settings.gradle @@ -37,6 +37,7 @@ String[] includes = [ ":serialization:base64", ":serialization:encapsulator", ":serialization:typed_serializer", + ":serialization:mapper", ":startup:plugin", ":startup:launcher",