Skip to content

Commit

Permalink
update JSON prettifier style
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Feb 11, 2024
1 parent 9f9c73f commit 1a22c76
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sunnychung.application.multiplatform.hellohttp.manager

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.sunnychung.application.multiplatform.hellohttp.model.ProtocolApplication
import com.sunnychung.application.multiplatform.hellohttp.parser.JsonParser

class PrettifierManager {
private val registrations: MutableSet<PrettifierRegistration> = mutableSetOf()
Expand All @@ -17,7 +18,7 @@ class PrettifierManager {
prettifier = Prettifier(
formatName = "JSON (Prettified)",
prettify = {
jacksonObjectMapper().readTree(it).toPrettyString()
JsonParser(it).prettify()
}
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.sunnychung.application.multiplatform.hellohttp.parser

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

class JsonParser(jsonBytes: ByteArray) {
val tree = jacksonObjectMapper().readTree(jsonBytes)

constructor(json: String) : this(json.encodeToByteArray())

fun prettify(): String {
return buildString {
fun indent(level: Int) {
if (level > 0) {
append(" ".repeat(2 * level))
}
}

fun StringBuilder.transverse(node: JsonNode, indentLevel: Int) {
if (node.isArray) {
append('[')
if (node.size() <= 20 && node.all { it.isValueNode }) {
node.forEachIndexed { i, it ->
if (i > 0) {
append(", ")
}
transverse(it, 0)
}
} else {
append("\n")
node.forEachIndexed { i, it ->
if (i > 0) {
append(",\n")
}
indent(indentLevel + 1)
transverse(it, indentLevel + 1)
}
append("\n")
indent(indentLevel)
}
append(']')
} else if (node.isObject) {
if (node.isEmpty) {
append("{}")
} else {
append("{\n")
node.fields().withIndex().forEach { (i, it) ->
if (i > 0) {
append(",\n")
}
indent(indentLevel + 1)
append("\"${it.key}\": ")
transverse(it.value, indentLevel + 1)
}
append("\n")
indent(indentLevel)
append('}')
}
} else if (node.isValueNode) {
append(node.toPrettyString())
} else {
throw RuntimeException("what is this? -- $node")
}
}

transverse(tree, 0)
}
}
}

0 comments on commit 1a22c76

Please sign in to comment.