From ead9188ffe6b8f8376788019ad86f749fe679ce1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 22 Mar 2020 18:26:03 +0600 Subject: [PATCH] version 0.2.0 --- CHANGELOG.md | 11 +++++++++-- build.gradle | 2 +- gradle.properties | 4 ++-- gradle/wrapper/gradle-wrapper.properties | 2 +- .../kotlin/com/insanusmokrassar/sdi/HelperTools.kt | 9 ++++++++- .../sdi/ModuleDeserializerStrategy.kt | 7 ++++--- .../insanusmokrassar/sdi/utils/DependencyResolver.kt | 9 ++++++--- .../com/insanusmokrassar/sdi/utils/JsonAdaptations.kt | 2 ++ .../kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt | 6 +++--- 9 files changed, 36 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efd50b0..18fcd70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,19 @@ # SDI changelogs -## 0.1.2 +## 0.2.0 + +* `Kotlin`: `1.3.61` -> `1.3.70` +* `Kotlin Serialization`: `0.14.0` -> `0.20.0` + +## 0.1.0 + +### 0.1.2 * All reflection support methods are internal * Added `loadModule` functions for more useful working with modules loading * `Module` now is not serializable and can be created only via `loadModule` functions (at least, for some time) -## 0.1.1 +### 0.1.1 * Added opportunity to create objects inside of config: * Now it is possible to construct object without usage of dependency on `@ContextualSerialization` place diff --git a/build.gradle b/build.gradle index 4d80c9c..694f5d8 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ plugins { id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" } -project.version = "0.1.2" +project.version = "0.2.0" project.group = "com.insanusmokrassar" apply from: "publish.gradle" diff --git a/gradle.properties b/gradle.properties index abf984d..32a449c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ kotlin.code.style=official -kotlin_version=1.3.61 -kotlin_serialisation_runtime_version=0.14.0 +kotlin_version=1.3.70 +kotlin_serialisation_runtime_version=0.20.0 gradle_bintray_plugin_version=1.8.4 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 30b572c..8ef0669 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt index 4686f7a..f60ec5d 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt @@ -3,8 +3,15 @@ package com.insanusmokrassar.sdi import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.json.Json +internal val nonStrictJson = Json { + isLenient = true + ignoreUnknownKeys = true + serializeSpecialFloatingPointValues = true + useArrayPolymorphism = true +} + @ImplicitReflectionSerializer fun Json.loadModule(json: String): Module = parse(ModuleDeserializerStrategy, json) @ImplicitReflectionSerializer -fun loadModule(json: String): Module = Json.nonstrict.loadModule(json) +fun loadModule(json: String): Module = nonStrictJson.loadModule(json) diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt index 3313c4e..301d3e7 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt @@ -2,16 +2,17 @@ package com.insanusmokrassar.sdi import com.insanusmokrassar.sdi.utils.createModuleBasedOnConfigRoot import kotlinx.serialization.* -import kotlinx.serialization.internal.HashMapSerializer -import kotlinx.serialization.internal.StringSerializer +import kotlinx.serialization.builtins.MapSerializer +import kotlinx.serialization.builtins.serializer import kotlinx.serialization.json.* @ImplicitReflectionSerializer internal object ModuleDeserializerStrategy : DeserializationStrategy { - private val internalSerializer = HashMapSerializer(StringSerializer, ContextSerializer(Any::class)) + private val internalSerializer = MapSerializer(String.serializer(), ContextSerializer(Any::class)) override val descriptor: SerialDescriptor get() = internalSerializer.descriptor + @InternalSerializationApi override fun deserialize(decoder: Decoder): Module { val json = JsonObjectSerializer.deserialize(decoder) val jsonSerialFormat = createModuleBasedOnConfigRoot(json) diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt index 5ccaf93..042a909 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/DependencyResolver.kt @@ -6,6 +6,7 @@ import kotlinx.serialization.modules.SerializerAlreadyRegisteredException import kotlinx.serialization.modules.SerializersModuleBuilder import kotlin.reflect.KClass +@InternalSerializationApi @ImplicitReflectionSerializer internal class DependencyResolver( serialModuleBuilder: SerializersModuleBuilder, @@ -39,23 +40,25 @@ internal class DependencyResolver( val decoded = decoder.decodeSerializableValue(JsonElementSerializer) return when { decoded is JsonPrimitive && decoded.contentOrNull != null -> decoded.content.let { dependencyName -> + @Suppress("UNCHECKED_CAST") (dependencyGetter(dependencyName) as T).also { objectsCache[dependencyName] = it } } decoded is JsonArray -> { val serializer = resolveSerializerByPackageName(decoded.getPrimitive(0).content) + @Suppress("UNCHECKED_CAST") formatterGetter().fromJson(serializer, decoded[1]) as T } else -> formatterGetter().fromJson(originalSerializer, decoded) } } - override fun serialize(encoder: Encoder, obj: T) { + override fun serialize(encoder: Encoder, value: T) { objectsCache.keys.firstOrNull { - objectsCache[it] === obj + objectsCache[it] === value } ?.also { dependencyName -> encoder.encodeString(dependencyName) - } ?: originalSerializer.serialize(encoder, obj) + } ?: originalSerializer.serialize(encoder, value) } } diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt index 2d57c3d..5d54b5b 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt @@ -1,6 +1,7 @@ package com.insanusmokrassar.sdi.utils import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.json.* import kotlinx.serialization.modules.SerializerAlreadyRegisteredException import kotlinx.serialization.modules.SerializersModule @@ -21,6 +22,7 @@ private fun JsonElement.resolvePackageName(currentKey: String, otherDependencies } } +@InternalSerializationApi @ImplicitReflectionSerializer internal fun createModuleBasedOnConfigRoot(jsonObject: JsonObject): Json { lateinit var caches: Map Any> diff --git a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt b/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt index e32d946..8f9c0bc 100644 --- a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt +++ b/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt @@ -1,18 +1,18 @@ package com.insanusmokrassar.sdi -import kotlinx.io.InputStream import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.json.Json import java.io.File +import java.io.InputStream @ImplicitReflectionSerializer fun Json.loadModule(stream: InputStream) = loadModule(stream.reader().readText()) @ImplicitReflectionSerializer -fun loadModule(stream: InputStream) = Json.nonstrict.loadModule(stream.reader().readText()) +fun loadModule(stream: InputStream) = nonStrictJson.loadModule(stream.reader().readText()) @ImplicitReflectionSerializer fun Json.loadModule(file: File) = loadModule(file.inputStream()) @ImplicitReflectionSerializer -fun loadModule(file: File) = Json.nonstrict.loadModule(file.inputStream()) +fun loadModule(file: File) = nonStrictJson.loadModule(file.inputStream())