Update meta serialization rules
This commit is contained in:
parent
de476fb273
commit
ee5afcdafe
@ -6,6 +6,7 @@
|
||||
|
||||
### Changed
|
||||
- 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
|
||||
|
||||
|
@ -9,7 +9,7 @@ plugins {
|
||||
|
||||
allprojects {
|
||||
group = "space.kscience"
|
||||
version = "0.6.2-dev-2"
|
||||
version = "0.6.2-dev-3"
|
||||
}
|
||||
|
||||
subprojects {
|
||||
|
@ -6,7 +6,7 @@ package space.kscience.dataforge.io
|
||||
import io.ktor.utils.io.core.Input
|
||||
import io.ktor.utils.io.core.Output
|
||||
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.meta.Meta
|
||||
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 {
|
||||
|
||||
override fun writeMeta(output: Output, meta: Meta, descriptor: MetaDescriptor?) {
|
||||
val jsonObject = meta.toJson(descriptor)
|
||||
output.writeUtf8String(json.encodeToString(JsonObject.serializer(), jsonObject))
|
||||
val jsonElement = meta.toJson(descriptor)
|
||||
output.writeUtf8String(json.encodeToString(JsonElement.serializer(), jsonElement))
|
||||
}
|
||||
|
||||
override fun readMeta(input: Input, descriptor: MetaDescriptor?): Meta {
|
||||
|
@ -64,33 +64,32 @@ private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): J
|
||||
JsonObject(pairs.toMap())
|
||||
}
|
||||
|
||||
public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonObject {
|
||||
val element = toJsonWithIndex(descriptor, null)
|
||||
return if (element is JsonObject) {
|
||||
element
|
||||
} else {
|
||||
buildJsonObject {
|
||||
put("@value", element)
|
||||
}
|
||||
}
|
||||
public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonElement {
|
||||
return toJsonWithIndex(descriptor, null)
|
||||
// val element = toJsonWithIndex(descriptor, null)
|
||||
// return if (element is JsonObject) {
|
||||
// element
|
||||
// } else {
|
||||
// buildJsonObject {
|
||||
// put("@value", element)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a Json primitive to a [Value]
|
||||
*/
|
||||
public fun JsonPrimitive.toValue(descriptor: MetaDescriptor?): Value {
|
||||
return when (this) {
|
||||
public fun JsonPrimitive.toValue(descriptor: MetaDescriptor?): Value = when (this) {
|
||||
JsonNull -> Null
|
||||
else -> {
|
||||
if (isString) {
|
||||
StringValue(content)
|
||||
content.asValue()
|
||||
} else {
|
||||
//consider using LazyParse
|
||||
content.parseValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package space.kscience.dataforge.meta
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
import space.kscience.dataforge.names.asName
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@ -12,4 +13,33 @@ class MetaSerializationTest {
|
||||
val meta = Json.decodeFromString(MetaSerializer, 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