additional subclasses in loadModule

This commit is contained in:
InsanusMokrassar 2020-08-12 17:21:42 +06:00
parent 1fc3dba66a
commit dc9fd28553
5 changed files with 47 additions and 12 deletions

View File

@ -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

View File

@ -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)

View File

@ -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<Module> {
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) }
)

View File

@ -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<String?, String?>
@ -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<String, () -> 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()
}

View File

@ -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)