diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c47d4e..627957a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * `Kotlin`: `1.3.71` -> `1.3.72` * Add `SerializersModuleBuilder#optionalContextual` for more comfortable usage of `contextual` +* All `loadModule` functions now able to retrieve additional classes to inspect inside of serializer of module ## 0.2.0 diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt index dd4c591..19aae93 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/HelperTools.kt @@ -3,6 +3,7 @@ package com.insanusmokrassar.sdi import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.json.Json import kotlinx.serialization.modules.SerializersModuleBuilder +import kotlin.reflect.KClass internal val nonStrictJson = Json { isLenient = true @@ -10,17 +11,23 @@ internal val nonStrictJson = Json { serializeSpecialFloatingPointValues = true } -@ImplicitReflectionSerializer -private val ModuleDeserializerStrategyWithNullOptional = ModuleDeserializerStrategy() - @ImplicitReflectionSerializer fun Json.loadModule( json: String, + vararg additionalClassesToInclude: KClass<*>, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null -): Module = parse(if (moduleBuilder != null) ModuleDeserializerStrategy(moduleBuilder) else ModuleDeserializerStrategyWithNullOptional, json) +): Module = parse( + if (moduleBuilder != null) { + ModuleDeserializerStrategy(moduleBuilder, *additionalClassesToInclude) + } else { + ModuleDeserializerStrategy(additionalClassesToInclude = *additionalClassesToInclude) + }, + json +) @ImplicitReflectionSerializer fun loadModule( json: String, + vararg additionalClassesToInclude: KClass<*>, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null -): Module = nonStrictJson.loadModule(json, moduleBuilder) +): Module = nonStrictJson.loadModule(json, *additionalClassesToInclude, moduleBuilder = moduleBuilder) diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt index 33b0aea..da0db8e 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/ModuleDeserializerStrategy.kt @@ -6,10 +6,12 @@ import kotlinx.serialization.builtins.MapSerializer import kotlinx.serialization.builtins.serializer import kotlinx.serialization.json.* import kotlinx.serialization.modules.SerializersModuleBuilder +import kotlin.reflect.KClass @ImplicitReflectionSerializer internal class ModuleDeserializerStrategy( - private val moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null + private val moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null, + private vararg val additionalClassesToInclude: KClass<*> ) : DeserializationStrategy { private val internalSerializer = MapSerializer(String.serializer(), ContextSerializer(Any::class)) override val descriptor: SerialDescriptor @@ -17,7 +19,12 @@ internal class ModuleDeserializerStrategy( override fun deserialize(decoder: Decoder): Module { val json = JsonObjectSerializer.deserialize(decoder) - val jsonSerialFormat = createModuleBasedOnConfigRoot(json, moduleBuilder, decoder.context) + val jsonSerialFormat = createModuleBasedOnConfigRoot( + json, + moduleBuilder, + decoder.context, + *additionalClassesToInclude + ) val resultJson = JsonObject( json.keys.associateWith { JsonPrimitive(it) } ) diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt index 30653bf..2771ce5 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt @@ -4,6 +4,7 @@ import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.json.* import kotlinx.serialization.modules.* +import kotlin.reflect.KClass private typealias PackageOrOtherDependencyNamePair = Pair @@ -25,7 +26,8 @@ private fun JsonElement.resolvePackageName(currentKey: String, otherDependencies internal fun createModuleBasedOnConfigRoot( jsonObject: JsonObject, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null, - baseContext: SerialModule + baseContext: SerialModule, + vararg additionalClassesToInclude: KClass<*> ): Json { lateinit var caches: Map Any> lateinit var jsonStringFormat: Json @@ -85,6 +87,19 @@ internal fun createModuleBasedOnConfigRoot( // here we are thinking that already registered } } + additionalClassesToInclude.forEach { + try { + DependencyResolver( + this, + it, + { jsonStringFormat } + ) { + caches.getValue(it).invoke() + } + } catch (e: AlreadyRegisteredException) { + // here we are thinking that already registered + } + } if (moduleBuilder != null) { moduleBuilder() } diff --git a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt b/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt index 57d7c48..538aed2 100644 --- a/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt +++ b/src/jvmMain/kotlin/com/insanusmokrassar/sdi/JVMHelperTools.kt @@ -1,5 +1,6 @@ package com.insanusmokrassar.sdi +import kotlin.reflect.KClass import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.json.Json import kotlinx.serialization.modules.SerializersModuleBuilder @@ -9,23 +10,27 @@ import java.io.InputStream @ImplicitReflectionSerializer fun Json.loadModule( stream: InputStream, + vararg additionalClassesToInclude: KClass<*>, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null -) = loadModule(stream.reader().readText(), moduleBuilder) +) = loadModule(stream.reader().readText(), *additionalClassesToInclude, moduleBuilder = moduleBuilder) @ImplicitReflectionSerializer fun loadModule( stream: InputStream, + vararg additionalClassesToInclude: KClass<*>, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null -) = nonStrictJson.loadModule(stream, moduleBuilder) +) = nonStrictJson.loadModule(stream, *additionalClassesToInclude, moduleBuilder = moduleBuilder) @ImplicitReflectionSerializer fun Json.loadModule( file: File, + vararg additionalClassesToInclude: KClass<*>, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null -) = loadModule(file.inputStream(), moduleBuilder) +) = loadModule(file.inputStream(), *additionalClassesToInclude, moduleBuilder = moduleBuilder) @ImplicitReflectionSerializer fun loadModule( file: File, + vararg additionalClassesToInclude: KClass<*>, moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null -) = nonStrictJson.loadModule(file, moduleBuilder) +) = nonStrictJson.loadModule(file, *additionalClassesToInclude, moduleBuilder = moduleBuilder)