mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-12-14 10:15:49 +00:00
add docs to mapper serialization and allow to use them as supertypes
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
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
|
||||
|
||||
/**
|
||||
* Use this serializer when you have deserializable type [I] and want to map it to some [O] in process of
|
||||
* deserialization
|
||||
*
|
||||
* @param base Serializer for [I]
|
||||
* @param deserialize Will be used in [deserialize] method to convert deserialized by [base] [I] to [O]
|
||||
*/
|
||||
open class MapperDeserializationStrategy<I, O>(
|
||||
private val base: DeserializationStrategy<I>,
|
||||
private val deserialize: (I) -> O
|
||||
) : DeserializationStrategy<O> {
|
||||
override val descriptor: SerialDescriptor = base.descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): O {
|
||||
return deserialize(base.deserialize(decoder))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user