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
## 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
* 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)
## 0.1.1
### 0.1.1
* Added opportunity to create objects inside of config:
* 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"
}
project.version = "0.1.2"
project.version = "0.2.0"
project.group = "com.insanusmokrassar"
apply from: "publish.gradle"

View File

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

View File

@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
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.json.Json
internal val nonStrictJson = Json {
isLenient = true
ignoreUnknownKeys = true
serializeSpecialFloatingPointValues = true
useArrayPolymorphism = true
}
@ImplicitReflectionSerializer
fun Json.loadModule(json: String): Module = parse(ModuleDeserializerStrategy, json)
@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 kotlinx.serialization.*
import kotlinx.serialization.internal.HashMapSerializer
import kotlinx.serialization.internal.StringSerializer
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.*
@ImplicitReflectionSerializer
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
get() = internalSerializer.descriptor
@InternalSerializationApi
override fun deserialize(decoder: Decoder): Module {
val json = JsonObjectSerializer.deserialize(decoder)
val jsonSerialFormat = createModuleBasedOnConfigRoot(json)

View File

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

View File

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

View File

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