mirror of
https://github.com/gkd-kit/gkd.git
synced 2024-11-16 03:32:38 +08:00
perf: 优化 json5 输出格式
This commit is contained in:
parent
787398210d
commit
2534e81827
|
@ -62,23 +62,30 @@ fun convertJsonElementToJson5(element: JsonElement, indent: Int = 2): String {
|
|||
}
|
||||
|
||||
is JsonObject -> {
|
||||
// Handle JSON objects
|
||||
val entries = element.entries.joinToString(",\n") { (key, value) ->
|
||||
// If key is a valid identifier, no quotes are needed
|
||||
if (key.matches(json5IdentifierReg)) {
|
||||
"$key: ${convertJsonElementToJson5(value, indent)}"
|
||||
} else {
|
||||
"${escapeString(key)}: ${convertJsonElementToJson5(value, indent)}"
|
||||
}
|
||||
}.lineSequence().map { l -> spaces + l }.joinToString("\n")
|
||||
"{\n$entries\n}"
|
||||
if (element.isEmpty()) {
|
||||
"{}"
|
||||
} else {
|
||||
val entries = element.entries.joinToString(",\n") { (key, value) ->
|
||||
// If key is a valid identifier, no quotes are needed
|
||||
if (key.matches(json5IdentifierReg)) {
|
||||
"$key: ${convertJsonElementToJson5(value, indent)}"
|
||||
} else {
|
||||
"${escapeString(key)}: ${convertJsonElementToJson5(value, indent)}"
|
||||
}
|
||||
}.lineSequence().map { l -> spaces + l }.joinToString("\n")
|
||||
"{\n$entries\n}"
|
||||
}
|
||||
}
|
||||
|
||||
is JsonArray -> {
|
||||
val elements =
|
||||
element.joinToString(",\n") { convertJsonElementToJson5(it, indent) }
|
||||
.lineSequence().map { l -> spaces + l }.joinToString("\n")
|
||||
"[\n$elements\n]"
|
||||
if (element.isEmpty()) {
|
||||
"[]"
|
||||
} else {
|
||||
val elements =
|
||||
element.joinToString(",\n") { convertJsonElementToJson5(it, indent) }
|
||||
.lineSequence().map { l -> spaces + l }.joinToString("\n")
|
||||
"[\n$elements\n]"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user