Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions androidApp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.Material.Light.NoActionBar">
<profileable android:shell="true"/>
<activity
android:name="com.sebastianneubauer.jsontreesample.MainActivity"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
# Enabled parallel sync for Gradle 9.4+
org.gradle.tooling.parallel=true
# configuration cache is disabled because it breaks publishing
# https://github.com/gradle/gradle/issues/22779
org.gradle.configuration-cache=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ private fun JsonTreeList(
val coloredText = rememberPrimitiveText(
key = item.key,
value = item.value,
type = item.type,
colors = colors,
isLastItem = item.isLastItem,
searchOccurrence = searchOccurrence,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.sebastianneubauer.jsontree

import kotlinx.serialization.json.JsonPrimitive

internal sealed interface JsonTreeElement {
val id: String
val level: Int
Expand All @@ -14,9 +12,12 @@ internal sealed interface JsonTreeElement {
override val level: Int,
override val isLastItem: Boolean,
val key: String?,
val value: JsonPrimitive,
val type: Type,
val value: String,
val parentType: ParentType,
) : JsonTreeElement
) : JsonTreeElement {
enum class Type { STRING, BOOLEAN, NUMBER, OTHER }
}

sealed interface Collapsable : JsonTreeElement {
val state: TreeState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import com.sebastianneubauer.jsontree.CollapsableType
import com.sebastianneubauer.jsontree.JsonTreeElement
import com.sebastianneubauer.jsontree.JsonTreeElement.Primitive.Type
import com.sebastianneubauer.jsontree.JsonTreeElement.ParentType
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.floatOrNull
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.longOrNull

@Composable
internal fun rememberCollapsableDiffText(
Expand Down Expand Up @@ -59,25 +54,14 @@ internal fun rememberCollapsableDiffText(
@Composable
internal fun rememberPrimitiveDiffText(
key: String?,
value: JsonPrimitive,
value: String,
type: Type,
colors: JsonTreeDiffColors,
highlightColor: Color,
isLastItem: Boolean,
parentType: ParentType,
diffIndices: List<Pair<Int, Int>>?,
): AnnotatedString {
val valueColor = remember(value) {
when {
value.isString -> colors.stringValueColor
value.booleanOrNull != null -> colors.booleanValueColor
value.doubleOrNull != null ||
value.intOrNull != null ||
value.floatOrNull != null ||
value.longOrNull != null -> colors.numberValueColor
else -> colors.nullValueColor
}
}

return remember(colors) {
buildAnnotatedString {
key?.let { key ->
Expand All @@ -92,8 +76,19 @@ internal fun rememberPrimitiveDiffText(
}
}

val valueColor = when(type) {
Type.STRING -> colors.stringValueColor
Type.BOOLEAN -> colors.booleanValueColor
Type.NUMBER -> colors.numberValueColor
Type.OTHER -> colors.nullValueColor
}

withStyle(SpanStyle(color = valueColor)) {
append(value.toString())
if(type == Type.STRING) {
append("\"$value\"")
} else {
append(value)
}
}

if (!isLastItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ private fun rememberText(
is JsonTreeElement.Primitive -> rememberPrimitiveDiffText(
key = jsonTreeElement.key,
value = jsonTreeElement.value,
type = jsonTreeElement.type,
isLastItem = jsonTreeElement.isLastItem,
parentType = jsonTreeElement.parentType,
colors = colors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ internal class JsonTreeSearch(
return when (this) {
is JsonTreeElement.Primitive -> {
if (parentType != JsonTreeElement.ParentType.ARRAY) {
searchRegex.findRanges(key) + searchRegex.findRanges(value.content, isKey = false)
searchRegex.findRanges(key) + searchRegex.findRanges(value, isKey = false)
} else {
searchRegex.findRanges(value.content, isKey = false)
searchRegex.findRanges(value, isKey = false)
}
}
is JsonTreeElement.Collapsable.Array -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import com.sebastianneubauer.jsontree.CollapsableType
import com.sebastianneubauer.jsontree.JsonTreeElement.Primitive.Type
import com.sebastianneubauer.jsontree.JsonTreeElement.ParentType
import com.sebastianneubauer.jsontree.TreeColors
import com.sebastianneubauer.jsontree.TreeState
import com.sebastianneubauer.jsontree.search.SearchOccurrence
import jsontree.jsontree.generated.resources.Res
import jsontree.jsontree.generated.resources.jsontree_collapsable_items
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.floatOrNull
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.longOrNull
import org.jetbrains.compose.resources.pluralStringResource

@Composable
Expand Down Expand Up @@ -115,30 +110,20 @@ internal fun rememberCollapsableText(
@Composable
internal fun rememberPrimitiveText(
key: String?,
value: JsonPrimitive,
value: String,
type: Type,
colors: TreeColors,
isLastItem: Boolean,
searchOccurrence: SearchOccurrence?,
searchOccurrenceSelectedRange: SearchOccurrence.Range?,
showIndices: Boolean,
parentType: ParentType,
): AnnotatedString {
val valueColor = remember(value) {
when {
value.isString -> colors.stringValueColor
value.booleanOrNull != null -> colors.booleanValueColor
value.doubleOrNull != null ||
value.intOrNull != null ||
value.floatOrNull != null ||
value.longOrNull != null -> colors.numberValueColor
else -> colors.nullValueColor
}
}

return remember(
key,
value,
colors,
type,
isLastItem,
showIndices,
searchOccurrence,
Expand Down Expand Up @@ -182,9 +167,19 @@ internal fun rememberPrimitiveText(
}

val keyOffset = this.length
val valueColor = when(type) {
Type.STRING -> colors.stringValueColor
Type.BOOLEAN -> colors.booleanValueColor
Type.NUMBER -> colors.numberValueColor
Type.OTHER -> colors.nullValueColor
}

withStyle(SpanStyle(color = valueColor)) {
append(value.toString())
if(type == Type.STRING) {
append("\"$value\"")
} else {
append(value)
}
}

searchOccurrence
Expand All @@ -199,7 +194,7 @@ internal fun rememberPrimitiveText(
// add an offset for the key which is already appended to the string
// add 1 to the range if the value is a string because it has quotes around it
// add 1 to the end because it is exclusive
val stringQuoteOffset = if (value.isString) 1 else 0
val stringQuoteOffset = if (type == Type.STRING) 1 else 0
addStyle(
style = SpanStyle(background = color),
start = keyOffset + valueRange.range.first + stringQuoteOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import com.sebastianneubauer.jsontree.JsonTreeElement.Collapsable.Object
import com.sebastianneubauer.jsontree.JsonTreeElement.EndBracket
import com.sebastianneubauer.jsontree.JsonTreeElement.ParentType
import com.sebastianneubauer.jsontree.JsonTreeElement.Primitive
import com.sebastianneubauer.jsontree.JsonTreeElement.Primitive.Type
import com.sebastianneubauer.jsontree.TreeState
import com.sebastianneubauer.jsontree.endBracket
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.doubleOrNull
import kotlinx.serialization.json.floatOrNull
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.longOrNull

internal enum class Expansion {
/**
Expand Down Expand Up @@ -204,9 +210,18 @@ internal fun JsonElement.toJsonTreeElement(
id = idGenerator.incrementAndGet().toString(),
level = level,
key = key,
value = this,
value = content,
isLastItem = isLastItem,
parentType = parentType,
type = when {
isString -> Type.STRING
booleanOrNull != null -> Type.BOOLEAN
doubleOrNull != null ||
intOrNull != null ||
floatOrNull != null ||
longOrNull != null -> Type.NUMBER
else -> Type.OTHER
},
)
}
is JsonArray -> {
Expand Down Expand Up @@ -278,10 +293,15 @@ internal fun JsonTreeElement.toRenderString(): String {
} else {
"["
}
is Primitive -> if (key != null && parentType != ParentType.ARRAY) {
"\"$key\": $value" + if (isLastItem) "" else ","
} else {
"$value" + if (isLastItem) "" else ","
is Primitive -> {
val isString = type == Type.STRING
val quotedValue = if(isString) "\"$value\"" else value

if (key != null && parentType != ParentType.ARRAY) {
"\"$key\": $quotedValue" + if (isLastItem) "" else ","
} else {
quotedValue + if (isLastItem) "" else ","
}
}
is EndBracket -> when (type) {
EndBracket.Type.ARRAY -> if (!isLastItem) "]," else "]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sebastianneubauer.jsontree

import com.sebastianneubauer.jsontree.JsonTreeElement.Collapsable.Array
import com.sebastianneubauer.jsontree.JsonTreeElement.Collapsable.Object
import com.sebastianneubauer.jsontree.JsonTreeElement.Primitive.Type
import com.sebastianneubauer.jsontree.util.Expansion
import com.sebastianneubauer.jsontree.util.collapse
import com.sebastianneubauer.jsontree.util.expand
Expand Down Expand Up @@ -252,7 +253,8 @@ public class JsonTreeElementExtensionsTest {
level = 0,
isLastItem = false,
key = "myProp",
value = JsonPrimitive("test"),
value = "test",
type = Type.STRING,
parentType = JsonTreeElement.ParentType.OBJECT
)

Expand All @@ -271,7 +273,8 @@ public class JsonTreeElementExtensionsTest {
level = 0,
isLastItem = true,
key = "myProp",
value = JsonPrimitive("test"),
value = "test",
type = Type.STRING,
parentType = JsonTreeElement.ParentType.OBJECT
)

Expand All @@ -290,7 +293,8 @@ public class JsonTreeElementExtensionsTest {
level = 0,
isLastItem = false,
key = "0",
value = JsonPrimitive(42),
value = "42",
type = Type.NUMBER,
parentType = JsonTreeElement.ParentType.ARRAY
)

Expand All @@ -309,7 +313,8 @@ public class JsonTreeElementExtensionsTest {
level = 0,
isLastItem = true,
key = "0",
value = JsonPrimitive(42),
value = "42",
type = Type.NUMBER,
parentType = JsonTreeElement.ParentType.ARRAY
)

Expand All @@ -328,7 +333,8 @@ public class JsonTreeElementExtensionsTest {
level = 0,
isLastItem = false,
key = null,
value = JsonPrimitive(true),
value = "true",
type = Type.BOOLEAN,
parentType = JsonTreeElement.ParentType.NONE
)

Expand All @@ -347,7 +353,8 @@ public class JsonTreeElementExtensionsTest {
level = 0,
isLastItem = true,
key = null,
value = JsonPrimitive(false),
value = "false",
type = Type.BOOLEAN,
parentType = JsonTreeElement.ParentType.NONE
)

Expand Down Expand Up @@ -433,7 +440,8 @@ public class JsonTreeElementExtensionsTest {
level = 2,
isLastItem = false,
key = "0",
value = JsonPrimitive("value1"),
value = "value1",
type = Type.STRING,
parentType = JsonTreeElement.ParentType.ARRAY
)

Expand All @@ -442,7 +450,8 @@ public class JsonTreeElementExtensionsTest {
level = 3,
isLastItem = true,
key = "primitive2",
value = JsonPrimitive("value2"),
value = "value2",
type = Type.STRING,
parentType = JsonTreeElement.ParentType.OBJECT
)

Expand Down Expand Up @@ -488,7 +497,8 @@ public class JsonTreeElementExtensionsTest {
level = 2,
isLastItem = false,
key = "0",
value = JsonPrimitive("value1"),
value = "value1",
type = Type.STRING,
parentType = JsonTreeElement.ParentType.ARRAY
)

Expand All @@ -497,7 +507,8 @@ public class JsonTreeElementExtensionsTest {
level = 3,
isLastItem = true,
key = "primitive2",
value = JsonPrimitive("value2"),
value = "value2",
type = Type.STRING,
parentType = JsonTreeElement.ParentType.OBJECT
)

Expand Down
Loading
Loading