add mapper serializer

This commit is contained in:
InsanusMokrassar 2023-03-09 21:55:07 +06:00
parent 4be1d93f60
commit f5f7511781
4 changed files with 34 additions and 0 deletions

View File

@ -2,6 +2,10 @@
## 0.17.4
* `Serialization`:
* `Mapper`:
* Module inited
## 0.17.3
* `Common`:

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,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<I, O>(
private val base: KSerializer<I>,
private val serialize: (O) -> I,
private val deserialize: (I) -> O
) : KSerializer<O> {
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))
}
}

View File

@ -37,6 +37,7 @@ String[] includes = [
":serialization:base64",
":serialization:encapsulator",
":serialization:typed_serializer",
":serialization:mapper",
":startup:plugin",
":startup:launcher",