JsonComposeViewer
1.0.0indexedRenders JSON elements as a collapsible, syntax‑colored tree with expandable objects/arrays, type‑based coloring, CompositionLocal-driven indentation, and configurable JsonViewerStyle (colors, spacing, counts).
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 viewerJsonComposeViewer expects a JsonElement from kotlinx.serialization.json.Json. A typical flow is:
JsonElementJsonComposeViewer 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(
showElementCount = ,
),
initiallyExpanded =
)
}
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.
JsonComposeViewer API overviewThe 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 for layoutInternally, JsonComposeViewer:
CompositionLocalJsonObjectView for JsonObjectYou normally only use JsonComposeViewer and JsonViewerStyle directly.
JsonViewerStyleJsonViewerStyle lets you customize how the viewer looks:
net.tactware.jsoncomposeviewer.coreKey properties (see core/src/commonMain/.../JsonViewerStyle.kt for full list):
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.ktsample/src/commonMain/kotlin/net/tactware/jsoncomposeviewer/sample/JsonViewerSampleScreen.ktRun from the project root:
./gradlew :sample:run
Or run MainKt from your IDE in the sample module.
sample/src/androidMain/kotlin/net/tactware/jsoncomposeviewer/sample/MainActivity.ktJsonViewerSampleScreen() 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 test
Open 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).
commonMain UI built with Compose MultiplatformCompositionLocal instead of manual parametersJsonViewerStyle:
showElementCount to toggle whether headers show the number of fields/elementsModifierstyle – A JsonViewerStyle instance to tweak appearanceinitiallyExpanded – Whether nested objects/arrays start expandedJsonArrayView for JsonArrayJsonPrimitiveView for JsonPrimitivekeyColor – Color for JSON object keysstringColor, numberColor, booleanColor, nullColorbackgroundColor – Background for collapsible headersindentation – Horizontal space added per nesting levelspacing – Vertical spacing between entriesborderColor, borderWidth, cornerRadiusfontSizeexpandIconColorshowElementCount – Controls header text:
{3} vs {}[5] vs []Surfaced from shared tags and platforms — no rankings paid for.