|
| 1 | +package cafe.adriel.bonsai.json |
| 2 | + |
| 3 | +import androidx.compose.ui.text.font.FontFamily |
| 4 | +import cafe.adriel.bonsai.core.BonsaiStyle |
| 5 | +import cafe.adriel.bonsai.core.node.Node |
| 6 | +import cafe.adriel.bonsai.core.node.SimpleBranchNode |
| 7 | +import cafe.adriel.bonsai.core.node.SimpleLeafNode |
| 8 | +import kotlinx.serialization.json.Json |
| 9 | +import kotlinx.serialization.json.JsonArray |
| 10 | +import kotlinx.serialization.json.JsonElement |
| 11 | +import kotlinx.serialization.json.JsonNull |
| 12 | +import kotlinx.serialization.json.JsonObject |
| 13 | +import kotlinx.serialization.json.JsonPrimitive |
| 14 | +import kotlinx.serialization.json.contentOrNull |
| 15 | + |
| 16 | +public fun JsonBonsaiStyle(): BonsaiStyle<JsonElement> = |
| 17 | + BonsaiStyle( |
| 18 | + nodeNameTextStyle = BonsaiStyle.DefaultNodeTextStyle.copy( |
| 19 | + fontFamily = FontFamily.Monospace |
| 20 | + ) |
| 21 | + ) |
| 22 | + |
| 23 | +public fun jsonNodes( |
| 24 | + json: String |
| 25 | +): List<Node<JsonElement>> = |
| 26 | + jsonNodes( |
| 27 | + key = "", |
| 28 | + jsonElement = Json.Default.parseToJsonElement(json), |
| 29 | + parent = null |
| 30 | + ) |
| 31 | + |
| 32 | +private fun jsonNodes( |
| 33 | + key: String, |
| 34 | + jsonElement: JsonElement, |
| 35 | + parent: Node<JsonElement>? |
| 36 | +): List<Node<JsonElement>> = |
| 37 | + listOf( |
| 38 | + when (jsonElement) { |
| 39 | + is JsonNull -> JsonPrimitiveNode(key, jsonElement, parent) |
| 40 | + is JsonPrimitive -> JsonPrimitiveNode(key, jsonElement, parent) |
| 41 | + is JsonObject -> JsonObjectNode(key, jsonElement, parent) |
| 42 | + is JsonArray -> JsonArrayNode(key, jsonElement, parent) |
| 43 | + } |
| 44 | + ) |
| 45 | + |
| 46 | +private fun JsonPrimitiveNode( |
| 47 | + key: String, |
| 48 | + jsonPrimitive: JsonPrimitive, |
| 49 | + parent: Node<JsonElement>? |
| 50 | +) = |
| 51 | + SimpleLeafNode( |
| 52 | + content = jsonPrimitive, |
| 53 | + name = "${getFormattedKey(key)}${getFormattedValue(jsonPrimitive)}", |
| 54 | + parent = parent |
| 55 | + ) |
| 56 | + |
| 57 | +private fun JsonObjectNode( |
| 58 | + key: String, |
| 59 | + jsonObject: JsonObject, |
| 60 | + parent: Node<JsonElement>? |
| 61 | +) = |
| 62 | + SimpleBranchNode( |
| 63 | + content = jsonObject, |
| 64 | + name = "${getFormattedKey(key)}{object}", |
| 65 | + parent = parent, |
| 66 | + children = { node -> |
| 67 | + jsonObject.entries.flatMap { (name, jsonElement) -> |
| 68 | + jsonNodes(name, jsonElement, node) |
| 69 | + } |
| 70 | + } |
| 71 | + ) |
| 72 | + |
| 73 | +private fun JsonArrayNode( |
| 74 | + key: String, |
| 75 | + jsonArray: JsonArray, |
| 76 | + parent: Node<JsonElement>? |
| 77 | +) = |
| 78 | + SimpleBranchNode( |
| 79 | + content = jsonArray, |
| 80 | + name = "${getFormattedKey(key)}[array]", |
| 81 | + parent = parent, |
| 82 | + children = { node -> |
| 83 | + jsonArray.flatMapIndexed { index, jsonElement -> |
| 84 | + jsonNodes(index.toString(), jsonElement, node) |
| 85 | + } |
| 86 | + } |
| 87 | + ) |
| 88 | + |
| 89 | +private fun getFormattedKey(key: String) = |
| 90 | + if (key.isBlank()) "" |
| 91 | + else "$key: " |
| 92 | + |
| 93 | +private fun getFormattedValue(jsonPrimitive: JsonPrimitive) = |
| 94 | + if (jsonPrimitive.isString) "\"${jsonPrimitive.contentOrNull}\"" |
| 95 | + else jsonPrimitive.contentOrNull |
0 commit comments