version 0.2.0

This commit is contained in:
InsanusMokrassar 2020-03-22 18:26:03 +06:00
parent e210693ce6
commit ead9188ffe
9 changed files with 36 additions and 16 deletions

View File

@ -1,12 +1,19 @@
# SDI changelogs # 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 * All reflection support methods are internal
* Added `loadModule` functions for more useful working with modules loading * 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) * `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: * Added opportunity to create objects inside of config:
* Now it is possible to construct object without usage of dependency on `@ContextualSerialization` place * Now it is possible to construct object without usage of dependency on `@ContextualSerialization` place

View File

@ -17,7 +17,7 @@ plugins {
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
} }
project.version = "0.1.2" project.version = "0.2.0"
project.group = "com.insanusmokrassar" project.group = "com.insanusmokrassar"
apply from: "publish.gradle" apply from: "publish.gradle"

View File

@ -1,6 +1,6 @@
kotlin.code.style=official kotlin.code.style=official
kotlin_version=1.3.61 kotlin_version=1.3.70
kotlin_serialisation_runtime_version=0.14.0 kotlin_serialisation_runtime_version=0.20.0
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4

View File

@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists 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

View File

@ -3,8 +3,15 @@ package com.insanusmokrassar.sdi
import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
internal val nonStrictJson = Json {
isLenient = true
ignoreUnknownKeys = true
serializeSpecialFloatingPointValues = true
useArrayPolymorphism = true
}
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
fun Json.loadModule(json: String): Module = parse(ModuleDeserializerStrategy, json) fun Json.loadModule(json: String): Module = parse(ModuleDeserializerStrategy, json)
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
fun loadModule(json: String): Module = Json.nonstrict.loadModule(json) fun loadModule(json: String): Module = nonStrictJson.loadModule(json)

View File

@ -2,16 +2,17 @@ package com.insanusmokrassar.sdi
import com.insanusmokrassar.sdi.utils.createModuleBasedOnConfigRoot import com.insanusmokrassar.sdi.utils.createModuleBasedOnConfigRoot
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.internal.HashMapSerializer import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.internal.StringSerializer import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
internal object ModuleDeserializerStrategy : DeserializationStrategy<Module> { internal object ModuleDeserializerStrategy : DeserializationStrategy<Module> {
private val internalSerializer = HashMapSerializer(StringSerializer, ContextSerializer(Any::class)) private val internalSerializer = MapSerializer(String.serializer(), ContextSerializer(Any::class))
override val descriptor: SerialDescriptor override val descriptor: SerialDescriptor
get() = internalSerializer.descriptor get() = internalSerializer.descriptor
@InternalSerializationApi
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) val jsonSerialFormat = createModuleBasedOnConfigRoot(json)

View File

@ -6,6 +6,7 @@ import kotlinx.serialization.modules.SerializerAlreadyRegisteredException
import kotlinx.serialization.modules.SerializersModuleBuilder import kotlinx.serialization.modules.SerializersModuleBuilder
import kotlin.reflect.KClass import kotlin.reflect.KClass
@InternalSerializationApi
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
internal class DependencyResolver<T : Any>( internal class DependencyResolver<T : Any>(
serialModuleBuilder: SerializersModuleBuilder, serialModuleBuilder: SerializersModuleBuilder,
@ -39,23 +40,25 @@ internal class DependencyResolver<T : Any>(
val decoded = decoder.decodeSerializableValue(JsonElementSerializer) val decoded = decoder.decodeSerializableValue(JsonElementSerializer)
return when { return when {
decoded is JsonPrimitive && decoded.contentOrNull != null -> decoded.content.let { dependencyName -> decoded is JsonPrimitive && decoded.contentOrNull != null -> decoded.content.let { dependencyName ->
@Suppress("UNCHECKED_CAST")
(dependencyGetter(dependencyName) as T).also { (dependencyGetter(dependencyName) as T).also {
objectsCache[dependencyName] = it objectsCache[dependencyName] = it
} }
} }
decoded is JsonArray -> { decoded is JsonArray -> {
val serializer = resolveSerializerByPackageName(decoded.getPrimitive(0).content) val serializer = resolveSerializerByPackageName(decoded.getPrimitive(0).content)
@Suppress("UNCHECKED_CAST")
formatterGetter().fromJson(serializer, decoded[1]) as T formatterGetter().fromJson(serializer, decoded[1]) as T
} }
else -> formatterGetter().fromJson(originalSerializer, decoded) else -> formatterGetter().fromJson(originalSerializer, decoded)
} }
} }
override fun serialize(encoder: Encoder, obj: T) { override fun serialize(encoder: Encoder, value: T) {
objectsCache.keys.firstOrNull { objectsCache.keys.firstOrNull {
objectsCache[it] === obj objectsCache[it] === value
} ?.also { dependencyName -> } ?.also { dependencyName ->
encoder.encodeString(dependencyName) encoder.encodeString(dependencyName)
} ?: originalSerializer.serialize(encoder, obj) } ?: originalSerializer.serialize(encoder, value)
} }
} }

View File

@ -1,6 +1,7 @@
package com.insanusmokrassar.sdi.utils package com.insanusmokrassar.sdi.utils
import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
import kotlinx.serialization.modules.SerializerAlreadyRegisteredException import kotlinx.serialization.modules.SerializerAlreadyRegisteredException
import kotlinx.serialization.modules.SerializersModule import kotlinx.serialization.modules.SerializersModule
@ -21,6 +22,7 @@ private fun JsonElement.resolvePackageName(currentKey: String, otherDependencies
} }
} }
@InternalSerializationApi
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
internal fun createModuleBasedOnConfigRoot(jsonObject: JsonObject): Json { internal fun createModuleBasedOnConfigRoot(jsonObject: JsonObject): Json {
lateinit var caches: Map<String, () -> Any> lateinit var caches: Map<String, () -> Any>

View File

@ -1,18 +1,18 @@
package com.insanusmokrassar.sdi package com.insanusmokrassar.sdi
import kotlinx.io.InputStream
import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.io.File import java.io.File
import java.io.InputStream
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
fun Json.loadModule(stream: InputStream) = loadModule(stream.reader().readText()) fun Json.loadModule(stream: InputStream) = loadModule(stream.reader().readText())
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
fun loadModule(stream: InputStream) = Json.nonstrict.loadModule(stream.reader().readText()) fun loadModule(stream: InputStream) = nonStrictJson.loadModule(stream.reader().readText())
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
fun Json.loadModule(file: File) = loadModule(file.inputStream()) fun Json.loadModule(file: File) = loadModule(file.inputStream())
@ImplicitReflectionSerializer @ImplicitReflectionSerializer
fun loadModule(file: File) = Json.nonstrict.loadModule(file.inputStream()) fun loadModule(file: File) = nonStrictJson.loadModule(file.inputStream())