Merge pull request #59 from InsanusMokrassar/0.4.35

0.4.35
This commit is contained in:
InsanusMokrassar 2021-04-15 14:19:31 +06:00 committed by GitHub
commit 9663c1ca64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 3 deletions

View File

@ -1,5 +1,13 @@
# Changelog # Changelog
## 0.4.35
* `Versions`:
* `Kotlin Exposed`: `0.30.1` -> `0.30.2`
* `Serialization`:
* `TypedSerializer`:
* Project has been inited
## 0.4.34 ## 0.4.34
* `Versions`: * `Versions`:

View File

@ -9,7 +9,7 @@ android.enableJetifier=true
kotlin_version=1.4.32 kotlin_version=1.4.32
kotlin_coroutines_version=1.4.3 kotlin_coroutines_version=1.4.3
kotlin_serialisation_core_version=1.1.0 kotlin_serialisation_core_version=1.1.0
kotlin_exposed_version=0.30.1 kotlin_exposed_version=0.30.2
ktor_version=1.5.3 ktor_version=1.5.3
@ -44,5 +44,5 @@ dokka_version=1.4.30
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.4.34 version=0.4.35
android_code_version=38 android_code_version=39

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,76 @@
package dev.inmo.micro_utils.serialization.typed_serializer
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlin.reflect.KClass
class TypedSerializer<T : Any>(
kClass: KClass<T>,
presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
) : KSerializer<T> {
private val serializers = presetSerializers.toMutableMap()
@InternalSerializationApi
override val descriptor: SerialDescriptor = buildSerialDescriptor(
"TextSourceSerializer",
SerialKind.CONTEXTUAL
) {
element("type", String.serializer().descriptor)
element("value", ContextualSerializer(kClass).descriptor)
}
@InternalSerializationApi
override fun deserialize(decoder: Decoder): T {
return decoder.decodeStructure(descriptor) {
var type: String? = null
lateinit var result: T
while (true) {
when (val index = decodeElementIndex(descriptor)) {
0 -> type = decodeStringElement(descriptor, 0)
1 -> {
require(type != null) { "Type is null, but it is expected that was inited already" }
result = decodeSerializableElement(
descriptor,
1,
serializers.getValue(type)
)
}
CompositeDecoder.DECODE_DONE -> break
else -> error("Unexpected index: $index")
}
}
result
}
}
@InternalSerializationApi
private fun <O: T> CompositeEncoder.encode(value: O) {
encodeSerializableElement(descriptor, 1, value::class.serializer() as KSerializer<O>, value)
}
@InternalSerializationApi
override fun serialize(encoder: Encoder, value: T) {
encoder.encodeStructure(descriptor) {
val valueSerializer = value::class.serializer()
val type = serializers.keys.first { serializers[it] == valueSerializer }
encodeStringElement(descriptor, 0, type)
encode(value)
}
}
fun <O: T> include(type: String, serializer: KSerializer<O>) {
require(type !in serializers.keys)
serializers[type] = serializer
}
fun exclude(type: String) {
require(type !in serializers.keys)
serializers.remove(type)
}
}
inline fun <reified T : Any> TypedSerializer(
presetSerializers: Map<String, KSerializer<out T>> = emptyMap()
) = TypedSerializer(T::class, presetSerializers)

View File

@ -0,0 +1,40 @@
package dev.inmo.micro_utils.serialization.typed_serializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
class TypedSerializerTests {
interface Example {
val number: Number
}
val serialFormat = Json { }
@Serializable
data class Example1(override val number: Long) : Example
@Serializable
data class Example2(override val number: Double) : Example
@Test
fun testThatSerializerWorksCorrectly() {
val serializer = TypedSerializer(
mapOf(
"long" to Example1.serializer(),
"double" to Example2.serializer()
)
)
val value1 = Example1(Random.nextLong())
val value2 = Example2(Random.nextDouble())
val list = listOf(value1, value2)
val serialized = serialFormat.encodeToString(ListSerializer(serializer), list)
val deserialized = serialFormat.decodeFromString(ListSerializer(serializer), serialized)
assertEquals(list, deserialized)
}
}

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.serialization.typed_serializer"/>

View File

@ -26,6 +26,7 @@ String[] includes = [
":android:alerts:recyclerview", ":android:alerts:recyclerview",
":serialization:base64", ":serialization:base64",
":serialization:encapsulator", ":serialization:encapsulator",
":serialization:typed_serializer",
":dokka" ":dokka"
] ]