
Renders JSON elements as a collapsible, syntax‑colored tree with expandable objects/arrays, type‑based coloring, CompositionLocal-driven indentation, and configurable JsonViewerStyle (colors, spacing, counts).
JsonComposeViewer is a Kotlin Multiplatform component for rendering JSON data using Jetpack Compose / Compose Multiplatform.
It takes a kotlinx.serialization.json.JsonElement and renders it as a collapsible, syntax‑colored tree, with:
JsonViewerStyle to control colors, spacing, and whether to show field/element countsThis repository contains:
core/ – the JsonComposeViewer librarysample/ – a small multiplatform sample app that demonstrates the viewercommonMain UI built with Compose MultiplatformCompositionLocal instead of manual parametersJsonViewerStyle:
showElementCount to toggle whether headers show the number of fields/elementsJsonComposeViewer expects a JsonElement from kotlinx.serialization.json.Json. A typical flow is:
JsonElement
JsonComposeViewer from your Compose UIExample:
import androidx.compose.runtime.Composable
import kotlinx.serialization.json.Json
import net.tactware.jsoncomposeviewer.core.JsonComposeViewer
import net.tactware.jsoncomposeviewer.core.JsonViewerStyle
@Composable
fun MyJsonScreen(jsonString: String) {
val element = Json.parseToJsonElement(jsonString)
JsonComposeViewer(
jsonElement = element,
style = JsonViewerStyle(
// Customize as needed
showElementCount = false, // default; set true to show counts
),
initiallyExpanded = true
)
}Note: Publishing coordinates for this library may change; for now, treat this repository as the source of truth and depend on it via a composite build or by copying the
coremodule into your project.
The main entry point is the JsonComposeViewer composable:
net.tactware.jsoncomposeviewer.core
@Composable
fun JsonComposeViewer(
jsonElement: JsonElement,
modifier: Modifier = Modifier,
style: JsonViewerStyle = JsonViewerStyle(),
initiallyExpanded: Boolean = false
)jsonElement – The JsonElement to render (object, array, or primitive)modifier – Usual Compose Modifier for layoutstyle – A JsonViewerStyle instance to tweak appearanceinitiallyExpanded – Whether nested objects/arrays start expandedInternally, JsonComposeViewer:
CompositionLocal
JsonObjectView for JsonObject
JsonArrayView for JsonArray
JsonPrimitiveView for JsonPrimitive
You normally only use JsonComposeViewer and JsonViewerStyle directly.
JsonViewerStyle lets you customize how the viewer looks:
net.tactware.jsoncomposeviewer.core
Key properties (see core/src/commonMain/.../JsonViewerStyle.kt for full list):
keyColor – Color for JSON object keysstringColor, numberColor, booleanColor, nullColor
backgroundColor – Background for collapsible headersindentation – Horizontal space added per nesting levelspacing – Vertical spacing between entriesborderColor, borderWidth, cornerRadius
fontSizeexpandIconColorshowElementCount – Controls header text:
{3} vs {}
[5] vs []
Example:
val style = JsonViewerStyle(
showElementCount = true, // show field/element counts in headers
indentation = 20.dp,
spacing = 6.dp
)
JsonComposeViewer(
jsonElement = element,
style = style,
initiallyExpanded = false
)The sample module demonstrates JsonComposeViewer on multiple platforms.
sample/src/jvmMain/kotlin/net/tactware/jsoncomposeviewer/sample/Main.kt
sample/src/commonMain/kotlin/net/tactware/jsoncomposeviewer/sample/JsonViewerSampleScreen.kt
Run from the project root:
./gradlew :sample:runOr run MainKt from your IDE in the sample module.
sample/src/androidMain/kotlin/net/tactware/jsoncomposeviewer/sample/MainActivity.kt
JsonViewerSampleScreen() as its content.Open the project in Android Studio or IntelliJ IDEA and run the sample Android configuration on a device/emulator.
./gradlew build./gradlew testOpen the project in IntelliJ IDEA or Android Studio for best Kotlin Multiplatform support.
Kotlin and Gradle versions are controlled via:
gradle/libs.versions.tomlgradle.propertiesUnless otherwise specified, this project is provided under the license declared in the repository root (add or reference your license file/text here).
JsonComposeViewer is a Kotlin Multiplatform component for rendering JSON data using Jetpack Compose / Compose Multiplatform.
It takes a kotlinx.serialization.json.JsonElement and renders it as a collapsible, syntax‑colored tree, with:
JsonViewerStyle to control colors, spacing, and whether to show field/element countsThis repository contains:
core/ – the JsonComposeViewer librarysample/ – a small multiplatform sample app that demonstrates the viewercommonMain UI built with Compose MultiplatformCompositionLocal instead of manual parametersJsonViewerStyle:
showElementCount to toggle whether headers show the number of fields/elementsJsonComposeViewer expects a JsonElement from kotlinx.serialization.json.Json. A typical flow is:
JsonElement
JsonComposeViewer from your Compose UIExample:
import androidx.compose.runtime.Composable
import kotlinx.serialization.json.Json
import net.tactware.jsoncomposeviewer.core.JsonComposeViewer
import net.tactware.jsoncomposeviewer.core.JsonViewerStyle
@Composable
fun MyJsonScreen(jsonString: String) {
val element = Json.parseToJsonElement(jsonString)
JsonComposeViewer(
jsonElement = element,
style = JsonViewerStyle(
// Customize as needed
showElementCount = false, // default; set true to show counts
),
initiallyExpanded = true
)
}Note: Publishing coordinates for this library may change; for now, treat this repository as the source of truth and depend on it via a composite build or by copying the
coremodule into your project.
The main entry point is the JsonComposeViewer composable:
net.tactware.jsoncomposeviewer.core
@Composable
fun JsonComposeViewer(
jsonElement: JsonElement,
modifier: Modifier = Modifier,
style: JsonViewerStyle = JsonViewerStyle(),
initiallyExpanded: Boolean = false
)jsonElement – The JsonElement to render (object, array, or primitive)modifier – Usual Compose Modifier for layoutstyle – A JsonViewerStyle instance to tweak appearanceinitiallyExpanded – Whether nested objects/arrays start expandedInternally, JsonComposeViewer:
CompositionLocal
JsonObjectView for JsonObject
JsonArrayView for JsonArray
JsonPrimitiveView for JsonPrimitive
You normally only use JsonComposeViewer and JsonViewerStyle directly.
JsonViewerStyle lets you customize how the viewer looks:
net.tactware.jsoncomposeviewer.core
Key properties (see core/src/commonMain/.../JsonViewerStyle.kt for full list):
keyColor – Color for JSON object keysstringColor, numberColor, booleanColor, nullColor
backgroundColor – Background for collapsible headersindentation – Horizontal space added per nesting levelspacing – Vertical spacing between entriesborderColor, borderWidth, cornerRadius
fontSizeexpandIconColorshowElementCount – Controls header text:
{3} vs {}
[5] vs []
Example:
val style = JsonViewerStyle(
showElementCount = true, // show field/element counts in headers
indentation = 20.dp,
spacing = 6.dp
)
JsonComposeViewer(
jsonElement = element,
style = style,
initiallyExpanded = false
)The sample module demonstrates JsonComposeViewer on multiple platforms.
sample/src/jvmMain/kotlin/net/tactware/jsoncomposeviewer/sample/Main.kt
sample/src/commonMain/kotlin/net/tactware/jsoncomposeviewer/sample/JsonViewerSampleScreen.kt
Run from the project root:
./gradlew :sample:runOr run MainKt from your IDE in the sample module.
sample/src/androidMain/kotlin/net/tactware/jsoncomposeviewer/sample/MainActivity.kt
JsonViewerSampleScreen() as its content.Open the project in Android Studio or IntelliJ IDEA and run the sample Android configuration on a device/emulator.
./gradlew build./gradlew testOpen the project in IntelliJ IDEA or Android Studio for best Kotlin Multiplatform support.
Kotlin and Gradle versions are controlled via:
gradle/libs.versions.tomlgradle.propertiesUnless otherwise specified, this project is provided under the license declared in the repository root (add or reference your license file/text here).