upgrade MapperSerializer

This commit is contained in:
InsanusMokrassar 2023-12-31 13:44:33 +06:00
parent bb466ce66c
commit c382423d77
3 changed files with 22 additions and 11 deletions

View File

@ -2,6 +2,10 @@
## 0.20.24
* `Serialization`:
* `Mapper`:
* `Mapper` pass decoder into callback of deserialization strategy
## 0.20.23
* `Versions`:

View File

@ -16,11 +16,16 @@ import kotlinx.serialization.encoding.Encoder
*/
open class MapperDeserializationStrategy<I, O>(
private val base: DeserializationStrategy<I>,
private val deserialize: (I) -> O
private val deserialize: (Decoder, I) -> O
) : DeserializationStrategy<O> {
override val descriptor: SerialDescriptor = base.descriptor
constructor(
base: DeserializationStrategy<I>,
deserialize: (I) -> O
) : this(base, { _, i -> deserialize(i) })
override fun deserialize(decoder: Decoder): O {
return deserialize(base.deserialize(decoder))
return deserialize(decoder, base.deserialize(decoder))
}
}

View File

@ -1,6 +1,8 @@
package dev.inmo.micro_utils.serialization.mapper
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@ -16,15 +18,15 @@ import kotlinx.serialization.encoding.Encoder
open class MapperSerializer<I, O>(
private val base: KSerializer<I>,
private val serialize: (O) -> I,
private val deserialize: (I) -> O
) : KSerializer<O> {
private val deserialize: (Decoder, I) -> O
) : KSerializer<O>,
DeserializationStrategy<O> by MapperDeserializationStrategy<I, O>(base, deserialize),
SerializationStrategy<O> by MapperSerializationStrategy<I, O>(base, serialize) {
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))
}
constructor(
base: KSerializer<I>,
serialize: (O) -> I,
deserialize: (I) -> O
) : this(base, serialize, { _, i -> deserialize(i) })
}