mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-01-11 02:09:56 +00:00
commit
cea65fc76e
@ -1,5 +1,13 @@
|
||||
# Changelog
|
||||
|
||||
## 0.17.4
|
||||
|
||||
* `Serialization`:
|
||||
* `Mapper`:
|
||||
* Module inited
|
||||
* `Versions`:
|
||||
* `Compose`: `1.3.1-rc02` -> `1.3.1`
|
||||
|
||||
## 0.17.3
|
||||
|
||||
* `Common`:
|
||||
|
@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.17.3
|
||||
android_code_version=185
|
||||
version=0.17.4
|
||||
android_code_version=186
|
||||
|
@ -6,7 +6,7 @@ kt-coroutines = "1.6.4"
|
||||
|
||||
kslog = "1.0.0"
|
||||
|
||||
jb-compose = "1.3.1-rc01"
|
||||
jb-compose = "1.3.1"
|
||||
jb-exposed = "0.41.1"
|
||||
jb-dokka = "1.7.20"
|
||||
|
||||
|
7
serialization/mapper/build.gradle
Normal file
7
serialization/mapper/build.gradle
Normal file
@ -0,0 +1,7 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
99
serialization/mapper/src/commonMain/kotlin/Extensions.kt
Normal file
99
serialization/mapper/src/commonMain/kotlin/Extensions.kt
Normal 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)
|
@ -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))
|
||||
}
|
||||
}
|
@ -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))
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
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
|
||||
|
||||
/**
|
||||
* 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 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))
|
||||
}
|
||||
}
|
1
serialization/mapper/src/main/AndroidManifest.xml
Normal file
1
serialization/mapper/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.micro_utils.serialization.mapper"/>
|
@ -37,6 +37,7 @@ String[] includes = [
|
||||
":serialization:base64",
|
||||
":serialization:encapsulator",
|
||||
":serialization:typed_serializer",
|
||||
":serialization:mapper",
|
||||
":startup:plugin",
|
||||
":startup:launcher",
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user