From 4d105caf246eee0a042665205fa0266afc4603b7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 19 Feb 2021 14:08:19 +0600 Subject: [PATCH] fixes of #6 and #7 --- CHANGELOG.md | 3 +++ .../kotlin/dev/inmo/sdi/ModuleDeserializerStrategy.kt | 1 + .../kotlin/dev/inmo/sdi/utils/JsonAdaptations.kt | 8 +++++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3f439e..d3bab17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.4.0 +* Fix of [#6](https://github.com/InsanusMokrassar/SDI/issues/6) +* Fix of [#7](https://github.com/InsanusMokrassar/SDI/issues/7) + ## 0.4.0-rc2 * `Kotlin`: `1.4.21` -> `1.4.30` diff --git a/src/commonMain/kotlin/dev/inmo/sdi/ModuleDeserializerStrategy.kt b/src/commonMain/kotlin/dev/inmo/sdi/ModuleDeserializerStrategy.kt index a22873e..33e085b 100644 --- a/src/commonMain/kotlin/dev/inmo/sdi/ModuleDeserializerStrategy.kt +++ b/src/commonMain/kotlin/dev/inmo/sdi/ModuleDeserializerStrategy.kt @@ -25,6 +25,7 @@ internal class ModuleDeserializerStrategy( override fun deserialize(decoder: Decoder): Module { val json = JsonObject.serializer().deserialize(decoder) val jsonSerialFormat = createModuleBasedOnConfigRoot( + (decoder as? JsonDecoder) ?.json, json, moduleBuilder, decoder.serializersModule, diff --git a/src/commonMain/kotlin/dev/inmo/sdi/utils/JsonAdaptations.kt b/src/commonMain/kotlin/dev/inmo/sdi/utils/JsonAdaptations.kt index de9580b..689efe2 100644 --- a/src/commonMain/kotlin/dev/inmo/sdi/utils/JsonAdaptations.kt +++ b/src/commonMain/kotlin/dev/inmo/sdi/utils/JsonAdaptations.kt @@ -2,6 +2,7 @@ package dev.inmo.sdi.utils import dev.inmo.sdi.getClassesForIncludingInSDI import kotlinx.serialization.InternalSerializationApi +import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.json.* import kotlinx.serialization.modules.* import kotlin.reflect.KClass @@ -26,7 +27,7 @@ private fun JsonElement.resolvePackageName(currentKey: String, otherDependencies is JsonObject -> if (currentKey in otherDependenciesKeys) { null to currentKey } else { - (namesToTheirClasses[currentKey] ?: currentKey) to null + (get("type") ?.jsonPrimitive ?.contentOrNull ?: namesToTheirClasses[currentKey] ?: currentKey) to null } is JsonArray -> return get(0).jsonPrimitive.contentOrNull ?.let { (namesToTheirClasses[it] ?: it) to null } ?: throw IllegalArgumentException("Value on first argument of dependency value must be its package as a string, but was provided ${get(0)}") } @@ -34,6 +35,7 @@ private fun JsonElement.resolvePackageName(currentKey: String, otherDependencies @InternalSerializationApi internal fun createModuleBasedOnConfigRoot( + baseJsonFormat: Json?, jsonObject: JsonObject, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null, baseContext: SerializersModule, @@ -43,7 +45,7 @@ internal fun createModuleBasedOnConfigRoot( lateinit var jsonStringFormat: Json caches = jsonObject.keys.map { key -> key to callback@{ - val elemValue = jsonObject.get(key) ?: throw IllegalStateException("Value for key $key must be provided, but was not") + val elemValue = jsonObject[key] ?: throw IllegalStateException("Value for key $key must be provided, but was not") val packageName: String = elemValue.resolvePackageName(key, jsonObject.keys).let { (packageName, otherDependencyName) -> when { @@ -115,7 +117,7 @@ internal fun createModuleBasedOnConfigRoot( } } ) - return Json { + return Json(baseJsonFormat ?: Json.Default) { useArrayPolymorphism = true serializersModule = context }.also {