From 1a22c7612ee6dffd36c8af4ef5f6f15adbcd71bf Mon Sep 17 00:00:00 2001 From: Sunny Chung Date: Mon, 12 Feb 2024 01:11:42 +0800 Subject: [PATCH] update JSON prettifier style --- .../hellohttp/manager/PrettifierManager.kt | 3 +- .../hellohttp/parser/JsonParser.kt | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/parser/JsonParser.kt diff --git a/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/manager/PrettifierManager.kt b/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/manager/PrettifierManager.kt index f6fc36d6..82d8b2f5 100644 --- a/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/manager/PrettifierManager.kt +++ b/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/manager/PrettifierManager.kt @@ -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 = mutableSetOf() @@ -17,7 +18,7 @@ class PrettifierManager { prettifier = Prettifier( formatName = "JSON (Prettified)", prettify = { - jacksonObjectMapper().readTree(it).toPrettyString() + JsonParser(it).prettify() } ) ) diff --git a/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/parser/JsonParser.kt b/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/parser/JsonParser.kt new file mode 100644 index 00000000..4208d278 --- /dev/null +++ b/src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/parser/JsonParser.kt @@ -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) + } + } +}