Update meta serialization rules
This commit is contained in:
parent
de476fb273
commit
ee5afcdafe
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Meta to Json serializer now serializes a single item with index as an array. It is important for plotly integration.
|
- Meta to Json serializer now serializes a single item with index as an array. It is important for plotly integration.
|
||||||
|
- Meta to Json serializes Meta without children a value as literal or array instead of an object with `@value` field.
|
||||||
|
|
||||||
### Deprecated
|
### Deprecated
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ plugins {
|
|||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
group = "space.kscience"
|
group = "space.kscience"
|
||||||
version = "0.6.2-dev-2"
|
version = "0.6.2-dev-3"
|
||||||
}
|
}
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
|
@ -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.JsonObject
|
import kotlinx.serialization.json.JsonElement
|
||||||
import space.kscience.dataforge.context.Context
|
import space.kscience.dataforge.context.Context
|
||||||
import space.kscience.dataforge.meta.Meta
|
import space.kscience.dataforge.meta.Meta
|
||||||
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
import space.kscience.dataforge.meta.descriptors.MetaDescriptor
|
||||||
@ -19,8 +19,8 @@ import space.kscience.dataforge.meta.toMeta
|
|||||||
public class JsonMetaFormat(private val json: Json = DEFAULT_JSON) : MetaFormat {
|
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 jsonElement = meta.toJson(descriptor)
|
||||||
output.writeUtf8String(json.encodeToString(JsonObject.serializer(), jsonObject))
|
output.writeUtf8String(json.encodeToString(JsonElement.serializer(), jsonElement))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun readMeta(input: Input, descriptor: MetaDescriptor?): Meta {
|
override fun readMeta(input: Input, descriptor: MetaDescriptor?): Meta {
|
||||||
|
@ -64,33 +64,32 @@ private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): J
|
|||||||
JsonObject(pairs.toMap())
|
JsonObject(pairs.toMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonObject {
|
public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonElement {
|
||||||
val element = toJsonWithIndex(descriptor, null)
|
return toJsonWithIndex(descriptor, null)
|
||||||
return if (element is JsonObject) {
|
// val element = toJsonWithIndex(descriptor, null)
|
||||||
element
|
// return if (element is JsonObject) {
|
||||||
} else {
|
// element
|
||||||
buildJsonObject {
|
// } else {
|
||||||
put("@value", element)
|
// buildJsonObject {
|
||||||
}
|
// put("@value", element)
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a Json primitive to a [Value]
|
* Convert a Json primitive to a [Value]
|
||||||
*/
|
*/
|
||||||
public fun JsonPrimitive.toValue(descriptor: MetaDescriptor?): Value {
|
public fun JsonPrimitive.toValue(descriptor: MetaDescriptor?): Value = when (this) {
|
||||||
return when (this) {
|
|
||||||
JsonNull -> Null
|
JsonNull -> Null
|
||||||
else -> {
|
else -> {
|
||||||
if (isString) {
|
if (isString) {
|
||||||
StringValue(content)
|
content.asValue()
|
||||||
} else {
|
} else {
|
||||||
//consider using LazyParse
|
//consider using LazyParse
|
||||||
content.parseValue()
|
content.parseValue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn this [JsonElement] into a [ListValue] with recursion or return null if it contains objects
|
* Turn this [JsonElement] into a [ListValue] with recursion or return null if it contains objects
|
||||||
|
@ -226,7 +226,7 @@ public fun <E : Enum<E>> E.asValue(): Value = EnumValue(this)
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create Value from String using closest match conversion
|
* Create Value from String using the closest match conversion
|
||||||
*/
|
*/
|
||||||
public fun String.parseValue(): Value {
|
public fun String.parseValue(): Value {
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package space.kscience.dataforge.meta
|
package space.kscience.dataforge.meta
|
||||||
|
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
import space.kscience.dataforge.names.asName
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
@ -12,4 +13,33 @@ class MetaSerializationTest {
|
|||||||
val meta = Json.decodeFromString(MetaSerializer, string)
|
val meta = Json.decodeFromString(MetaSerializer, string)
|
||||||
assertEquals(string, meta.value?.string)
|
assertEquals(string, meta.value?.string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun complexMeta() {
|
||||||
|
val meta = Meta {
|
||||||
|
"a" put 28.3
|
||||||
|
"b" put doubleArrayOf(1.0, 2.0, 3.2)
|
||||||
|
"child" put Meta {
|
||||||
|
"a" put "aString"
|
||||||
|
"sns[0]" put Meta {
|
||||||
|
"d" put 0
|
||||||
|
}
|
||||||
|
"sns[1]" put Meta {
|
||||||
|
"d" put 1
|
||||||
|
}
|
||||||
|
setIndexed(
|
||||||
|
"sns2".asName(),
|
||||||
|
listOf(
|
||||||
|
Meta { "d" put "first" },
|
||||||
|
Meta("53")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val string = Json.encodeToString(MetaSerializer, meta)
|
||||||
|
println(string)
|
||||||
|
val reconstructed = Json.decodeFromString(MetaSerializer, string)
|
||||||
|
assertEquals(meta, reconstructed)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user