mirror of
https://github.com/InsanusMokrassar/SDI.git
synced 2024-11-30 05:58:53 +00:00
additional subclasses in loadModule
This commit is contained in:
parent
1fc3dba66a
commit
dc9fd28553
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
* `Kotlin`: `1.3.71` -> `1.3.72`
|
* `Kotlin`: `1.3.71` -> `1.3.72`
|
||||||
* Add `SerializersModuleBuilder#optionalContextual` for more comfortable usage of `contextual`
|
* 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
|
## 0.2.0
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package com.insanusmokrassar.sdi
|
|||||||
import kotlinx.serialization.ImplicitReflectionSerializer
|
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.modules.SerializersModuleBuilder
|
import kotlinx.serialization.modules.SerializersModuleBuilder
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
internal val nonStrictJson = Json {
|
internal val nonStrictJson = Json {
|
||||||
isLenient = true
|
isLenient = true
|
||||||
@ -10,17 +11,23 @@ internal val nonStrictJson = Json {
|
|||||||
serializeSpecialFloatingPointValues = true
|
serializeSpecialFloatingPointValues = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
|
||||||
private val ModuleDeserializerStrategyWithNullOptional = ModuleDeserializerStrategy()
|
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
fun Json.loadModule(
|
fun Json.loadModule(
|
||||||
json: String,
|
json: String,
|
||||||
|
vararg additionalClassesToInclude: KClass<*>,
|
||||||
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
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
|
@ImplicitReflectionSerializer
|
||||||
fun loadModule(
|
fun loadModule(
|
||||||
json: String,
|
json: String,
|
||||||
|
vararg additionalClassesToInclude: KClass<*>,
|
||||||
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
||||||
): Module = nonStrictJson.loadModule(json, moduleBuilder)
|
): Module = nonStrictJson.loadModule(json, *additionalClassesToInclude, moduleBuilder = moduleBuilder)
|
||||||
|
@ -6,10 +6,12 @@ import kotlinx.serialization.builtins.MapSerializer
|
|||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.*
|
||||||
import kotlinx.serialization.modules.SerializersModuleBuilder
|
import kotlinx.serialization.modules.SerializersModuleBuilder
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
internal class ModuleDeserializerStrategy(
|
internal class ModuleDeserializerStrategy(
|
||||||
private val moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
private val moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null,
|
||||||
|
private vararg val additionalClassesToInclude: KClass<*>
|
||||||
) : DeserializationStrategy<Module> {
|
) : DeserializationStrategy<Module> {
|
||||||
private val internalSerializer = MapSerializer(String.serializer(), ContextSerializer(Any::class))
|
private val internalSerializer = MapSerializer(String.serializer(), ContextSerializer(Any::class))
|
||||||
override val descriptor: SerialDescriptor
|
override val descriptor: SerialDescriptor
|
||||||
@ -17,7 +19,12 @@ internal class ModuleDeserializerStrategy(
|
|||||||
|
|
||||||
override fun deserialize(decoder: Decoder): Module {
|
override fun deserialize(decoder: Decoder): Module {
|
||||||
val json = JsonObjectSerializer.deserialize(decoder)
|
val json = JsonObjectSerializer.deserialize(decoder)
|
||||||
val jsonSerialFormat = createModuleBasedOnConfigRoot(json, moduleBuilder, decoder.context)
|
val jsonSerialFormat = createModuleBasedOnConfigRoot(
|
||||||
|
json,
|
||||||
|
moduleBuilder,
|
||||||
|
decoder.context,
|
||||||
|
*additionalClassesToInclude
|
||||||
|
)
|
||||||
val resultJson = JsonObject(
|
val resultJson = JsonObject(
|
||||||
json.keys.associateWith { JsonPrimitive(it) }
|
json.keys.associateWith { JsonPrimitive(it) }
|
||||||
)
|
)
|
||||||
|
@ -4,6 +4,7 @@ import kotlinx.serialization.ImplicitReflectionSerializer
|
|||||||
import kotlinx.serialization.InternalSerializationApi
|
import kotlinx.serialization.InternalSerializationApi
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.*
|
||||||
import kotlinx.serialization.modules.*
|
import kotlinx.serialization.modules.*
|
||||||
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
private typealias PackageOrOtherDependencyNamePair = Pair<String?, String?>
|
private typealias PackageOrOtherDependencyNamePair = Pair<String?, String?>
|
||||||
|
|
||||||
@ -25,7 +26,8 @@ private fun JsonElement.resolvePackageName(currentKey: String, otherDependencies
|
|||||||
internal fun createModuleBasedOnConfigRoot(
|
internal fun createModuleBasedOnConfigRoot(
|
||||||
jsonObject: JsonObject,
|
jsonObject: JsonObject,
|
||||||
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null,
|
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null,
|
||||||
baseContext: SerialModule
|
baseContext: SerialModule,
|
||||||
|
vararg additionalClassesToInclude: KClass<*>
|
||||||
): Json {
|
): Json {
|
||||||
lateinit var caches: Map<String, () -> Any>
|
lateinit var caches: Map<String, () -> Any>
|
||||||
lateinit var jsonStringFormat: Json
|
lateinit var jsonStringFormat: Json
|
||||||
@ -85,6 +87,19 @@ internal fun createModuleBasedOnConfigRoot(
|
|||||||
// here we are thinking that already registered
|
// 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) {
|
if (moduleBuilder != null) {
|
||||||
moduleBuilder()
|
moduleBuilder()
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.insanusmokrassar.sdi
|
package com.insanusmokrassar.sdi
|
||||||
|
|
||||||
|
import kotlin.reflect.KClass
|
||||||
import kotlinx.serialization.ImplicitReflectionSerializer
|
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.modules.SerializersModuleBuilder
|
import kotlinx.serialization.modules.SerializersModuleBuilder
|
||||||
@ -9,23 +10,27 @@ import java.io.InputStream
|
|||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
fun Json.loadModule(
|
fun Json.loadModule(
|
||||||
stream: InputStream,
|
stream: InputStream,
|
||||||
|
vararg additionalClassesToInclude: KClass<*>,
|
||||||
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
||||||
) = loadModule(stream.reader().readText(), moduleBuilder)
|
) = loadModule(stream.reader().readText(), *additionalClassesToInclude, moduleBuilder = moduleBuilder)
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
fun loadModule(
|
fun loadModule(
|
||||||
stream: InputStream,
|
stream: InputStream,
|
||||||
|
vararg additionalClassesToInclude: KClass<*>,
|
||||||
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
||||||
) = nonStrictJson.loadModule(stream, moduleBuilder)
|
) = nonStrictJson.loadModule(stream, *additionalClassesToInclude, moduleBuilder = moduleBuilder)
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
fun Json.loadModule(
|
fun Json.loadModule(
|
||||||
file: File,
|
file: File,
|
||||||
|
vararg additionalClassesToInclude: KClass<*>,
|
||||||
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
||||||
) = loadModule(file.inputStream(), moduleBuilder)
|
) = loadModule(file.inputStream(), *additionalClassesToInclude, moduleBuilder = moduleBuilder)
|
||||||
|
|
||||||
@ImplicitReflectionSerializer
|
@ImplicitReflectionSerializer
|
||||||
fun loadModule(
|
fun loadModule(
|
||||||
file: File,
|
file: File,
|
||||||
|
vararg additionalClassesToInclude: KClass<*>,
|
||||||
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
moduleBuilder: (SerializersModuleBuilder.() -> Unit)? = null
|
||||||
) = nonStrictJson.loadModule(file, moduleBuilder)
|
) = nonStrictJson.loadModule(file, *additionalClassesToInclude, moduleBuilder = moduleBuilder)
|
||||||
|
Loading…
Reference in New Issue
Block a user