From 90a92c4121f49d3d631dcf1df2af9be088c17eea Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 1 Aug 2021 20:27:43 +0300 Subject: [PATCH] Don't serialize empty metas --- .../space/kscience/dataforge/meta/JsonMeta.kt | 27 +++++++++++-------- .../space/kscience/dataforge/meta/Meta.kt | 5 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt index 60320d7d..12f64762 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/JsonMeta.kt @@ -32,17 +32,22 @@ private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): J } else { val pairs: MutableList> = items.entries.groupBy { it.key.body - }.mapTo(ArrayList()) { (body, list) -> + }.mapNotNullTo(ArrayList()) { (body, list) -> val childDescriptor = descriptor?.children?.get(body) if (list.size == 1) { val (token, element) = list.first() - val child: JsonElement = element.toJsonWithIndex(childDescriptor, token.index) - body to child + //do not add empty element + if (!element.isEmpty()) { + val child: JsonElement = element.toJsonWithIndex(childDescriptor, token.index) + body to child + } else null } else { - val elements: List = list.sortedBy { it.key.index }.mapIndexed { index, entry -> - //Use index if it is not equal to the item order - val actualIndex = if (index.toString() != entry.key.index) entry.key.index else null - entry.value.toJsonWithIndex(childDescriptor, actualIndex) + val elements: List = list.sortedBy { it.key.index }.mapIndexedNotNull { index, entry -> + if (!entry.value.isEmpty()) { + //Use index if it is not equal to the item order + val actualIndex = if (index.toString() != entry.key.index) entry.key.index else null + entry.value.toJsonWithIndex(childDescriptor, actualIndex) + } else null } body to JsonArray(elements) } @@ -55,15 +60,15 @@ private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): J //Add value if needed if (value != null) { - pairs += Meta.VALUE_KEY to value!!.toJson(null) + pairs += Meta.VALUE_KEY to value!!.toJson(descriptor) } JsonObject(pairs.toMap()) } public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonObject { - val element = toJsonWithIndex(descriptor, null) - return if(element is JsonObject){ + val element = toJsonWithIndex(descriptor, null) + return if (element is JsonObject) { element } else { buildJsonObject { @@ -96,7 +101,7 @@ public fun JsonElement.toValueOrNull(descriptor: MetaDescriptor?): Value? = when is JsonPrimitive -> toValue(descriptor) is JsonObject -> get(Meta.VALUE_KEY)?.toValueOrNull(descriptor) is JsonArray -> { - if(isEmpty()) ListValue.EMPTY else { + if (isEmpty()) ListValue.EMPTY else { val values = map { it.toValueOrNull(descriptor) } values.map { it ?: return null }.asValue() } diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt index 45723682..21f9e5e3 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/meta/Meta.kt @@ -34,7 +34,7 @@ public interface Meta : MetaRepr, MetaProvider { public val value: Value? public val items: Map - override fun getMeta(name: Name): Meta?{ + override fun getMeta(name: Name): Meta? { tailrec fun Meta.find(name: Name): Meta? = if (name.isEmpty()) { this } else { @@ -197,7 +197,8 @@ public fun Meta.nodeSequence(): Sequence> = sequence { public operator fun Meta.iterator(): Iterator> = nodeSequence().iterator() -public fun Meta.isEmpty(): Boolean = this === Meta.EMPTY || (value == null && items.isEmpty()) +public fun Meta.isEmpty(): Boolean = this === Meta.EMPTY + || (value == null && (items.isEmpty() || items.values.all { it.isEmpty() })) /* Get operations*/