Don't serialize empty metas
This commit is contained in:
parent
b404615145
commit
90a92c4121
@ -32,17 +32,22 @@ private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): J
|
|||||||
} 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
|
||||||
}.mapTo(ArrayList()) { (body, list) ->
|
}.mapNotNullTo(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()
|
||||||
val child: JsonElement = element.toJsonWithIndex(childDescriptor, token.index)
|
//do not add empty element
|
||||||
body to child
|
if (!element.isEmpty()) {
|
||||||
|
val child: JsonElement = element.toJsonWithIndex(childDescriptor, token.index)
|
||||||
|
body to child
|
||||||
|
} else null
|
||||||
} else {
|
} else {
|
||||||
val elements: List<JsonElement> = list.sortedBy { it.key.index }.mapIndexed { index, entry ->
|
val elements: List<JsonElement> = list.sortedBy { it.key.index }.mapIndexedNotNull { index, entry ->
|
||||||
//Use index if it is not equal to the item order
|
if (!entry.value.isEmpty()) {
|
||||||
val actualIndex = if (index.toString() != entry.key.index) entry.key.index else null
|
//Use index if it is not equal to the item order
|
||||||
entry.value.toJsonWithIndex(childDescriptor, actualIndex)
|
val actualIndex = if (index.toString() != entry.key.index) entry.key.index else null
|
||||||
|
entry.value.toJsonWithIndex(childDescriptor, actualIndex)
|
||||||
|
} else null
|
||||||
}
|
}
|
||||||
body to JsonArray(elements)
|
body to JsonArray(elements)
|
||||||
}
|
}
|
||||||
@ -55,15 +60,15 @@ private fun Meta.toJsonWithIndex(descriptor: MetaDescriptor?, index: String?): J
|
|||||||
|
|
||||||
//Add value if needed
|
//Add value if needed
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
pairs += Meta.VALUE_KEY to value!!.toJson(null)
|
pairs += Meta.VALUE_KEY to value!!.toJson(descriptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject(pairs.toMap())
|
JsonObject(pairs.toMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonObject {
|
public fun Meta.toJson(descriptor: MetaDescriptor? = null): JsonObject {
|
||||||
val element = toJsonWithIndex(descriptor, null)
|
val element = toJsonWithIndex(descriptor, null)
|
||||||
return if(element is JsonObject){
|
return if (element is JsonObject) {
|
||||||
element
|
element
|
||||||
} else {
|
} else {
|
||||||
buildJsonObject {
|
buildJsonObject {
|
||||||
@ -96,7 +101,7 @@ public fun JsonElement.toValueOrNull(descriptor: MetaDescriptor?): Value? = when
|
|||||||
is JsonPrimitive -> toValue(descriptor)
|
is JsonPrimitive -> toValue(descriptor)
|
||||||
is JsonObject -> get(Meta.VALUE_KEY)?.toValueOrNull(descriptor)
|
is JsonObject -> get(Meta.VALUE_KEY)?.toValueOrNull(descriptor)
|
||||||
is JsonArray -> {
|
is JsonArray -> {
|
||||||
if(isEmpty()) ListValue.EMPTY else {
|
if (isEmpty()) ListValue.EMPTY else {
|
||||||
val values = map { it.toValueOrNull(descriptor) }
|
val values = map { it.toValueOrNull(descriptor) }
|
||||||
values.map { it ?: return null }.asValue()
|
values.map { it ?: return null }.asValue()
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ public interface Meta : MetaRepr, MetaProvider {
|
|||||||
public val value: Value?
|
public val value: Value?
|
||||||
public val items: Map<NameToken, Meta>
|
public val items: Map<NameToken, Meta>
|
||||||
|
|
||||||
override fun getMeta(name: Name): Meta?{
|
override fun getMeta(name: Name): Meta? {
|
||||||
tailrec fun Meta.find(name: Name): Meta? = if (name.isEmpty()) {
|
tailrec fun Meta.find(name: Name): Meta? = if (name.isEmpty()) {
|
||||||
this
|
this
|
||||||
} else {
|
} else {
|
||||||
@ -197,7 +197,8 @@ public fun Meta.nodeSequence(): Sequence<Pair<Name, Meta>> = sequence {
|
|||||||
|
|
||||||
public operator fun Meta.iterator(): Iterator<Pair<Name, Meta>> = nodeSequence().iterator()
|
public operator fun Meta.iterator(): Iterator<Pair<Name, Meta>> = 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*/
|
/* Get operations*/
|
||||||
|
Loading…
Reference in New Issue
Block a user