-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/json-tree-view' into 'main'
json tree view See merge request products/hello-http!15
- Loading branch information
Showing
11 changed files
with
386 additions
and
36 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
7 changes: 7 additions & 0 deletions
7
...jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/model/PrettifyResult.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,7 @@ | ||
package com.sunnychung.application.multiplatform.hellohttp.model | ||
|
||
data class PrettifyResult( | ||
val prettyString: String, | ||
val collapsableLineRange: List<IntRange> = emptyList(), | ||
val collapsableCharRange: List<IntRange> = emptyList(), | ||
) |
98 changes: 98 additions & 0 deletions
98
src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/parser/JsonParser.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,98 @@ | ||
package com.sunnychung.application.multiplatform.hellohttp.parser | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.sunnychung.application.multiplatform.hellohttp.model.PrettifyResult | ||
|
||
class JsonParser(jsonBytes: ByteArray) { | ||
val tree = jacksonObjectMapper().readTree(jsonBytes) | ||
|
||
constructor(json: String) : this(json.encodeToByteArray()) | ||
|
||
fun prettify(): PrettifyResult { | ||
var lineIndex = 0 | ||
val lineGroups = mutableListOf<IntRange>() | ||
val startLineStack = mutableListOf<Int>() | ||
val charGroups = mutableListOf<IntRange>() | ||
val startCharStack = mutableListOf<Int>() | ||
|
||
return PrettifyResult( | ||
prettyString = buildString { | ||
fun indent(level: Int) { | ||
if (level > 0) { | ||
append(" ".repeat(2 * level)) | ||
} | ||
} | ||
|
||
fun StringBuilder.transverse(node: JsonNode, indentLevel: Int) { | ||
if (node.isArray) { | ||
if (node.isEmpty) { | ||
append("[]") | ||
} else { | ||
startCharStack += lastIndex + 1 | ||
append('[') | ||
startLineStack += lineIndex | ||
if (node.size() <= 20 && node.all { it.isValueNode }) { | ||
node.forEachIndexed { i, it -> | ||
if (i > 0) { | ||
append(", ") | ||
} | ||
transverse(it, 0) | ||
} | ||
} else { | ||
append("\n") | ||
++lineIndex | ||
node.forEachIndexed { i, it -> | ||
if (i > 0) { | ||
append(",\n") | ||
++lineIndex | ||
} | ||
indent(indentLevel + 1) | ||
transverse(it, indentLevel + 1) | ||
} | ||
append("\n") | ||
++lineIndex | ||
indent(indentLevel) | ||
} | ||
append(']') | ||
lineGroups += startLineStack.removeLast() .. lineIndex | ||
charGroups += startCharStack.removeLast() .. lastIndex | ||
} | ||
} else if (node.isObject) { | ||
if (node.isEmpty) { | ||
append("{}") | ||
} else { | ||
startCharStack += lastIndex + 1 | ||
startLineStack += lineIndex | ||
append("{\n") | ||
++lineIndex | ||
node.fields().withIndex().forEach { (i, it) -> | ||
if (i > 0) { | ||
append(",\n") | ||
++lineIndex | ||
} | ||
indent(indentLevel + 1) | ||
append("\"${it.key}\": ") | ||
transverse(it.value, indentLevel + 1) | ||
} | ||
append("\n") | ||
++lineIndex | ||
indent(indentLevel) | ||
append('}') | ||
lineGroups += startLineStack.removeLast() .. lineIndex | ||
charGroups += startCharStack.removeLast() .. lastIndex | ||
} | ||
} else if (node.isValueNode) { | ||
append(node.toPrettyString()) | ||
} else { | ||
throw RuntimeException("what is this? -- $node") | ||
} | ||
} | ||
|
||
transverse(tree, 0) | ||
}, | ||
collapsableLineRange = lineGroups, | ||
collapsableCharRange = charGroups, | ||
) | ||
} | ||
} |
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
Oops, something went wrong.