Cleanup Json serializers

This commit is contained in:
Alexander Nozik 2021-08-01 19:17:20 +03:00
parent 7aec2f3547
commit b404615145
5 changed files with 424 additions and 541 deletions

View File

@ -4,7 +4,7 @@ plugins {
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.5.0-dev-5" version = "0.5.0-dev-6"
} }
subprojects { subprojects {

View File

@ -6,7 +6,7 @@ package space.kscience.dataforge.io
import io.ktor.utils.io.core.Input import io.ktor.utils.io.core.Input
import io.ktor.utils.io.core.Output import io.ktor.utils.io.core.Output
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject
import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Context
import space.kscience.dataforge.io.IOFormat.Companion.NAME_KEY import space.kscience.dataforge.io.IOFormat.Companion.NAME_KEY
import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.Meta
@ -25,7 +25,7 @@ public class JsonMetaFormat(private val json: Json = DEFAULT_JSON) : MetaFormat
override fun writeMeta(output: Output, meta: Meta, descriptor: MetaDescriptor?) { override fun writeMeta(output: Output, meta: Meta, descriptor: MetaDescriptor?) {
val jsonObject = meta.toJson(descriptor) val jsonObject = meta.toJson(descriptor)
output.writeUtf8String(json.encodeToString(JsonElement.serializer(), jsonObject)) output.writeUtf8String(json.encodeToString(JsonObject.serializer(), jsonObject))
} }
override fun toMeta(): Meta = Meta { override fun toMeta(): Meta = Meta {

File diff suppressed because it is too large Load Diff

View File

@ -61,7 +61,16 @@ private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): J
JsonObject(pairs.toMap()) JsonObject(pairs.toMap())
} }
public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonElement = toJsonWithIndex(descriptor, null) public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonObject {
val element = toJsonWithIndex(descriptor, null)
return if(element is JsonObject){
element
} else {
buildJsonObject {
put("@value", element)
}
}
}
/** /**
* Convert a Json primitive to a [Value] * Convert a Json primitive to a [Value]

View File

@ -5,7 +5,6 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonDecoder import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonEncoder import kotlinx.serialization.json.JsonEncoder
/** /**
@ -14,19 +13,17 @@ import kotlinx.serialization.json.JsonEncoder
public object MetaSerializer : KSerializer<Meta> { public object MetaSerializer : KSerializer<Meta> {
private val genericMetaSerializer = SealedMeta.serializer() private val genericMetaSerializer = SealedMeta.serializer()
private val jsonSerializer = JsonElement.serializer() override val descriptor: SerialDescriptor = genericMetaSerializer.descriptor
override val descriptor: SerialDescriptor = jsonSerializer.descriptor
override fun deserialize(decoder: Decoder): Meta = if (decoder is JsonDecoder) { override fun deserialize(decoder: Decoder): Meta = if (decoder is JsonDecoder) {
jsonSerializer.deserialize(decoder).toMeta() decoder.decodeJsonElement().toMeta()
} else { } else {
genericMetaSerializer.deserialize(decoder) genericMetaSerializer.deserialize(decoder)
} }
override fun serialize(encoder: Encoder, value: Meta) { override fun serialize(encoder: Encoder, value: Meta) {
if (encoder is JsonEncoder) { if (encoder is JsonEncoder) {
jsonSerializer.serialize(encoder, value.toJson()) encoder.encodeJsonElement(value.toJson())
} else { } else {
genericMetaSerializer.serialize(encoder, value.seal()) genericMetaSerializer.serialize(encoder, value.seal())
} }