add docs to mapper serialization and allow to use them as supertypes

This commit is contained in:
InsanusMokrassar 2023-03-09 22:21:11 +06:00
parent f5f7511781
commit b3f468f901
4 changed files with 159 additions and 1 deletions

View File

@ -0,0 +1,99 @@
package dev.inmo.micro_utils.serialization.mapper
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.serializer
import kotlin.reflect.KClass
/**
* Will create [MapperSerializationStrategy] to allow you to map [O] to [I] using [serialize] lambda during
* serialization process
*/
inline fun <reified I : Any, O> SerializationStrategy<I>.mapSerialization(
noinline serialize: (O) -> I
) = MapperSerializationStrategy(
this,
serialize
)
/**
* Will create [MapperDeserializationStrategy] to allow you to map [I] to [O] using [deserialize] lambda during
* deserialization process
*/
inline fun <reified I : Any, O> DeserializationStrategy<I>.mapDeserialization(
noinline deserialize: (I) -> O
) = MapperDeserializationStrategy(
this,
deserialize
)
/**
* Will create [MapperSerializer] to allow you to map [O] to [I] and vice verse using [serialize]/[deserialize] lambda during
* serialization/deserialization process
*/
inline fun <reified I : Any, O> KSerializer<I>.mapFullSerialization(
noinline serialize: (O) -> I,
noinline deserialize: (I) -> O
) = MapperSerializer(
this,
serialize,
deserialize
)
/**
* Will create [MapperSerializationStrategy] to allow you to map [O] to [I] using [serialize] lambda during
* serialization process
*/
@OptIn(InternalSerializationApi::class)
inline fun <reified I : Any, O> KClass<I>.mapSerialization(
serializer: SerializationStrategy<I> = serializer(),
noinline serialize: (O) -> I
) = serializer.mapSerialization(serialize)
/**
* Will create [MapperDeserializationStrategy] to allow you to map [I] to [O] using [deserialize] lambda during
* deserialization process
*/
@OptIn(InternalSerializationApi::class)
inline fun <reified I : Any, O> KClass<I>.mapDeserialization(
serializer: DeserializationStrategy<I> = serializer(),
noinline deserialize: (I) -> O
) = serializer.mapDeserialization(deserialize)
/**
* Will create [MapperSerializer] to allow you to map [O] to [I] and vice verse using [serialize]/[deserialize] lambda during
* serialization/deserialization process
*/
@OptIn(InternalSerializationApi::class)
inline fun <reified I : Any, O> KClass<I>.mapFullSerialization(
serializer: KSerializer<I> = serializer(),
noinline serialize: (O) -> I,
noinline deserialize: (I) -> O
) = serializer.mapFullSerialization(serialize, deserialize)
/**
* Will create [MapperSerializationStrategy] to allow you to map [O] to [I] using [serialize] lambda during
* serialization process
*/
inline fun <reified I : Any, O> mappedSerializationStrategy(
noinline serialize: (O) -> I,
) = serializer<I>().mapSerialization(serialize)
/**
* Will create [MapperDeserializationStrategy] to allow you to map [I] to [O] using [deserialize] lambda during
* deserialization process
*/
inline fun <reified I : Any, O> mappedDeserializationStrategy(
noinline deserialize: (I) -> O
) = serializer<I>().mapDeserialization(deserialize)
/**
* Will create [MapperSerializer] to allow you to map [O] to [I] and vice verse using [serialize]/[deserialize] lambda during
* serialization/deserialization process
*/
inline fun <reified I : Any, O> mappedSerializer(
noinline serialize: (O) -> I,
noinline deserialize: (I) -> O
) = serializer<I>().mapFullSerialization(serialize, deserialize)

View File

@ -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))
}
}

View File

@ -0,0 +1,25 @@
package dev.inmo.micro_utils.serialization.mapper
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 serializable type [I] and want to map it to some [O] in process of
* serialization
*
* @param base Serializer for [I]
* @param serialize Will be used in [serialize] method to convert incoming [O] to [I] and serialize with [base]
*/
open class MapperSerializationStrategy<I, O>(
private val base: SerializationStrategy<I>,
private val serialize: (O) -> I
) : SerializationStrategy<O> {
override val descriptor: SerialDescriptor = base.descriptor
override fun serialize(encoder: Encoder, value: O) {
base.serialize(encoder, serialize(value))
}
}

View File

@ -5,7 +5,15 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
class MapperSerializer<I, O>( /**
* Use this serializer when you have serializable type [I] and want to map it to some [O] in process of
* serialization/deserialization
*
* @param base Serializer for [I]
* @param serialize Will be used in [serialize] method to convert incoming [O] to [I] and serialize with [base]
* @param deserialize Will be used in [deserialize] method to convert deserialized by [base] [I] to [O]
*/
open class MapperSerializer<I, O>(
private val base: KSerializer<I>, private val base: KSerializer<I>,
private val serialize: (O) -> I, private val serialize: (O) -> I,
private val deserialize: (I) -> O private val deserialize: (I) -> O