-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fb065c
commit a737219
Showing
14 changed files
with
160 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
kotlin("plugin.serialization") | ||
id("com.android.library") | ||
id("org.jetbrains.compose") | ||
id("com.vanniktech.maven.publish") | ||
} | ||
|
||
kotlinMultiplatform() | ||
|
||
kotlin { | ||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
api(projects.bonsaiCore) | ||
api(libs.serialization) | ||
compileOnly(compose.foundation) | ||
compileOnly(compose.ui) | ||
} | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
POM_NAME=BonsaiJSON | ||
POM_ARTIFACT_ID=bonsai-json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="cafe.adriel.bonsai.json"/> |
95 changes: 95 additions & 0 deletions
95
bonsai-json/src/commonMain/kotlin/cafe/adriel/bonsai/json/JsonNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cafe.adriel.bonsai.json | ||
|
||
import androidx.compose.ui.text.font.FontFamily | ||
import cafe.adriel.bonsai.core.BonsaiStyle | ||
import cafe.adriel.bonsai.core.node.Node | ||
import cafe.adriel.bonsai.core.node.SimpleBranchNode | ||
import cafe.adriel.bonsai.core.node.SimpleLeafNode | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonArray | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonNull | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.contentOrNull | ||
|
||
public fun JsonBonsaiStyle(): BonsaiStyle<JsonElement> = | ||
BonsaiStyle( | ||
nodeNameTextStyle = BonsaiStyle.DefaultNodeTextStyle.copy( | ||
fontFamily = FontFamily.Monospace | ||
) | ||
) | ||
|
||
public fun jsonNodes( | ||
json: String | ||
): List<Node<JsonElement>> = | ||
jsonNodes( | ||
key = "", | ||
jsonElement = Json.Default.parseToJsonElement(json), | ||
parent = null | ||
) | ||
|
||
private fun jsonNodes( | ||
key: String, | ||
jsonElement: JsonElement, | ||
parent: Node<JsonElement>? | ||
): List<Node<JsonElement>> = | ||
listOf( | ||
when (jsonElement) { | ||
is JsonNull -> JsonPrimitiveNode(key, jsonElement, parent) | ||
is JsonPrimitive -> JsonPrimitiveNode(key, jsonElement, parent) | ||
is JsonObject -> JsonObjectNode(key, jsonElement, parent) | ||
is JsonArray -> JsonArrayNode(key, jsonElement, parent) | ||
} | ||
) | ||
|
||
private fun JsonPrimitiveNode( | ||
key: String, | ||
jsonPrimitive: JsonPrimitive, | ||
parent: Node<JsonElement>? | ||
) = | ||
SimpleLeafNode( | ||
content = jsonPrimitive, | ||
name = "${getFormattedKey(key)}${getFormattedValue(jsonPrimitive)}", | ||
parent = parent | ||
) | ||
|
||
private fun JsonObjectNode( | ||
key: String, | ||
jsonObject: JsonObject, | ||
parent: Node<JsonElement>? | ||
) = | ||
SimpleBranchNode( | ||
content = jsonObject, | ||
name = "${getFormattedKey(key)}{object}", | ||
parent = parent, | ||
children = { node -> | ||
jsonObject.entries.flatMap { (name, jsonElement) -> | ||
jsonNodes(name, jsonElement, node) | ||
} | ||
} | ||
) | ||
|
||
private fun JsonArrayNode( | ||
key: String, | ||
jsonArray: JsonArray, | ||
parent: Node<JsonElement>? | ||
) = | ||
SimpleBranchNode( | ||
content = jsonArray, | ||
name = "${getFormattedKey(key)}[array]", | ||
parent = parent, | ||
children = { node -> | ||
jsonArray.flatMapIndexed { index, jsonElement -> | ||
jsonNodes(index.toString(), jsonElement, node) | ||
} | ||
} | ||
) | ||
|
||
private fun getFormattedKey(key: String) = | ||
if (key.isBlank()) "" | ||
else "$key: " | ||
|
||
private fun getFormattedValue(jsonPrimitive: JsonPrimitive) = | ||
if (jsonPrimitive.isString) "\"${jsonPrimitive.contentOrNull}\"" | ||
else jsonPrimitive.contentOrNull |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters