Empty meta is represented by empty json object

This commit is contained in:
Alexander Nozik 2021-08-01 21:02:15 +03:00
parent 90a92c4121
commit 28a6914747
2 changed files with 9 additions and 13 deletions

View File

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

View File

@ -28,26 +28,22 @@ public fun Value.toJson(descriptor: MetaDescriptor? = null): JsonElement = when
private fun String.toJsonKey(descriptor: MetaDescriptor?) = descriptor?.attributes?.get("jsonName").string ?: toString() private fun String.toJsonKey(descriptor: MetaDescriptor?) = descriptor?.attributes?.get("jsonName").string ?: toString()
private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): JsonElement = if (items.isEmpty()) { private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): JsonElement = if (items.isEmpty()) {
value?.toJson(descriptor) ?: JsonNull value?.toJson(descriptor) ?: JsonObject(emptyMap())
} else { } else {
val pairs: MutableList<Pair<String, JsonElement>> = items.entries.groupBy { val pairs: MutableList<Pair<String, JsonElement>> = items.entries.groupBy {
it.key.body it.key.body
}.mapNotNullTo(ArrayList()) { (body, list) -> }.mapTo(ArrayList()) { (body, list) ->
val childDescriptor = descriptor?.children?.get(body) val childDescriptor = descriptor?.children?.get(body)
if (list.size == 1) { if (list.size == 1) {
val (token, element) = list.first() val (token, element) = list.first()
//do not add empty element //do not add empty element
if (!element.isEmpty()) { val child: JsonElement = element.toJsonWithIndex(childDescriptor, token.index)
val child: JsonElement = element.toJsonWithIndex(childDescriptor, token.index) body to child
body to child
} else null
} else { } else {
val elements: List<JsonElement> = list.sortedBy { it.key.index }.mapIndexedNotNull { index, entry -> val elements: List<JsonElement> = list.sortedBy { it.key.index }.mapIndexed { index, entry ->
if (!entry.value.isEmpty()) { //Use index if it is not equal to the item order
//Use index if it is not equal to the item order val actualIndex = if (index.toString() != entry.key.index) entry.key.index else null
val actualIndex = if (index.toString() != entry.key.index) entry.key.index else null entry.value.toJsonWithIndex(childDescriptor, actualIndex)
entry.value.toJsonWithIndex(childDescriptor, actualIndex)
} else null
} }
body to JsonArray(elements) body to JsonArray(elements)
} }