Created
December 20, 2017 20:33
-
-
Save bnorm/71c7973b4b3f928e855a183a3e56c791 to your computer and use it in GitHub Desktop.
Turns a Kotlin Data class toString() into formatted JSON-like output
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
fun Any?.toIndentString(): String { | |
val notFancy = toString() | |
return buildString(notFancy.length) { | |
var indent = 0 | |
fun StringBuilder.line() { | |
appendln() | |
repeat(2 * indent) { append(' ') } | |
} | |
for (char in notFancy) { | |
if (char == ' ') continue | |
when (char) { | |
')', ']' -> { | |
indent-- | |
line() | |
} | |
} | |
if (char == '=') append(' ') | |
append(char) | |
if (char == '=') append(' ') | |
when (char) { | |
'(', '[', ',' -> { | |
if (char != ',') indent++ | |
line() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refactored version