Skip to content

Instantly share code, notes, and snippets.

@mayankmkh
Last active September 12, 2025 07:39
Show Gist options
  • Select an option

  • Save mayankmkh/92084bdf2b59288d3e74c3735cccbf9f to your computer and use it in GitHub Desktop.

Select an option

Save mayankmkh/92084bdf2b59288d3e74c3735cccbf9f to your computer and use it in GitHub Desktop.
Pretty Print Kotlin Data Class
fun Any.prettyPrint(): String {
var indentLevel = 0
val indentWidth = 4
fun padding() = "".padStart(indentLevel * indentWidth)
val toString = toString()
val stringBuilder = StringBuilder(toString.length)
var i = 0
while (i < toString.length) {
when (val char = toString[i]) {
'(', '[', '{' -> {
indentLevel++
stringBuilder.appendLine(char).append(padding())
}
')', ']', '}' -> {
indentLevel--
stringBuilder.appendLine().append(padding()).append(char)
}
',' -> {
stringBuilder.appendLine(char).append(padding())
// ignore space after comma as we have added a newline
val nextChar = toString.getOrElse(i + 1) { char }
if (nextChar == ' ') i++
}
else -> {
stringBuilder.append(char)
}
}
i++
}
return stringBuilder.toString()
}
@mahdisml

mahdisml commented Jan 6, 2021

Copy link
Copy Markdown

Great Job !

@kollesnica-power

Copy link
Copy Markdown

Thank you!

@mayankmkh

Copy link
Copy Markdown
Author

Thanks @mahdisml, @kollesnica-power. Glad it helped!

@CWKSC

CWKSC commented Sep 29, 2021

Copy link
Copy Markdown

Change following be more pretty!

return stringBuilder.toString().replace("=", " = ")

@MaaxGr

MaaxGr commented Feb 14, 2022

Copy link
Copy Markdown

Thanks! That should be provided via webservice, so that everybody can use it ad hoc 😀

@mayankmkh

Copy link
Copy Markdown
Author

Great idea @MaaxGr 👍

@Nilzor

Nilzor commented Sep 9, 2022

Copy link
Copy Markdown

Just want to chime in that this method is pretty slow. Did some timings on a mid-sized object graph comparing these there:

  • Standard Kotlin toString
  • Using this library - prettyPrint
  • Using Jackson JSON serializing

Jackson was slowest the first run, but if you do this multiple times during the lifteime of an app, Jackson caches some reflection meta data that will be reused later for greater speed and prettyPrint ends up being slowest:

Run 1
Time printing n=100 with toString : 73 ms
Time printing n=100 with prettyprint : 371 ms
Time printing n=100 with jackson : 804 ms

Run 2 (same process)
Time printing n=100 with toString : 54
Time printing n=100with prettyprint : 256
Time printing n=100 with jackson : 81

Run 3 (same process)
Time printing n=100 with toString : 56
Time printing n=100 with prettyprint : 241
Time printing n=100 with jackson : 70

@mayankmkh

Copy link
Copy Markdown
Author

Thanks for the analysis @Nilzor. It was something I developed for debugging purposes and was never meant to be used in production hence provided as a gist instead of a library, so performance was not a consideration.
Would you mind trying to identify the cause and optimize this method? Also, it would be great if you could share the project with which the benchmark was performed.

@odonckers

Copy link
Copy Markdown

Thanks for posting this, it's been extremely helpful to my team and the logs recorded.

We did tweak your implementation slightly to get it closer to a "copy-and-paste" the logged model into the codebase. It's not perfect, but this gets us close:

fun Any?.toPrettyString(): String {
    if (this == null) return "(null)"

    var indentLevel = 0
    val indentWidth = 2

    fun padding() = "".padStart(indentLevel * indentWidth)

    val toString = toString()
    val stringBuilder = StringBuilder(toString.length)

    var nestingContext: Char? = null
    var i = 0

    // Replace standard '[' and '{' characters with Kotlin specific functions
    while (i < toString.length) {
        when (val char = toString[i]) {
            '(', '[', '{' -> {
                indentLevel++
                nestingContext = char
                stringBuilder
                    .appendLine(
                        when (char) {
                            '[' -> "listOf("
                            '{' -> "mapOf("
                            else -> '('
                        },
                    )
                    .append(padding())
            }

            ')', ']', '}' -> {
                indentLevel--
                nestingContext = null
                stringBuilder.appendLine().append(padding()).append(')')
            }

            ',' -> {
                stringBuilder.appendLine(char).append(padding())
                // ignore space after comma as we have added a newline
                val nextChar = toString.getOrElse(i + 1) { char }
                if (nextChar == ' ') i++
            }

            '=' -> {
                when (nestingContext) {
                    '{' -> stringBuilder.append(" to ")
                    else -> stringBuilder.append(" = ")
                }
            }

            else -> {
                stringBuilder.append(char)
            }
        }

        i++
    }

    return stringBuilder.toString()
}

If anyone notices any issues with this code or has a better idea, please feel free to tweak it and @ me. Thanks!

@tim4dev

tim4dev commented Aug 24, 2023

Copy link
Copy Markdown

Awesome, thx!

@mayankmkh

Copy link
Copy Markdown
Author

That's great @odonckers ! Glad it helped. I remember implementing a custom serializer using Kotlinx serialization to achieve the same. It was hacky and dirty but sufficed my use case. This is much easier and can be useful in many places. Awesome work!

@eskatos

eskatos commented Jan 22, 2024

Copy link
Copy Markdown

FWIW, this fails to produce a pretty output if you have strings that contain the , character

@thought-police-000

thought-police-000 commented Jan 31, 2024

Copy link
Copy Markdown

I cut this back and cleaned it up a little, and added some kotliny goodness, to make it a little cleaner and easier to read.

fun Any.pretty() = toString().let { toString ->
    var indentLevel = 0
    val indentWidth = 4
    var i = 0
    fun padding() = "".padStart(indentLevel * indentWidth)
    buildString {
        var ignoreSpace = false
        toString.onEach { char ->
            when (char) {
                '(', '[', '{' -> {
                    indentLevel++
                    appendLine(char)
                    append(padding())
                }

                ')', ']', '}' -> {
                    indentLevel--
                    appendLine()
                    append(padding())
                    append(char)
                }

                ',' -> {
                    appendLine(char)
                    append(padding())
                    ignoreSpace = true
                }
                ' ' -> {
                    if (!ignoreSpace) append(char)
                    ignoreSpace = false
                }

                else -> append(char)
            }
        }
    }
}

@thought-police-000

Copy link
Copy Markdown

With this method I don't think there's any way to get around a string containing a closing bracket.

data class Fish(val message: String)

Fish("bobby),otherValue=fakeData").pretty()

@jisungbin

Copy link
Copy Markdown

@thought-police-000 Thanks for sharing your awesome code!

@marctatham

Copy link
Copy Markdown

love it.

@johannesrave

johannesrave commented Oct 7, 2024

Copy link
Copy Markdown

@thought-police-000 definitely love the buildString builder, i didn't know about that!

regarding the ): you could count the " as well in a "stringLevel" to decide whether one is still inside a string or to decrement the indentation level? adds complexity obviously :/

@teenriot

Copy link
Copy Markdown

@thought-police-000 I cleaned it up more, replaced "=" by " = " and ruined it again by packing the code :)

fun Any.toPrettyDebugString(indentWidth : Int = 4) = buildString {
    fun StringBuilder.indent(level : Int) = append("".padStart(level * indentWidth))
    var ignoreSpace = false
    var indentLevel = 0
    this@toPrettyDebugString.toString().onEach {
        when (it) {
            '(', '[', '{' -> appendLine(it).indent(++indentLevel)
            ')', ']', '}' -> appendLine().indent(--indentLevel).append(it)
            ','           -> appendLine(it).indent(indentLevel).also { ignoreSpace = true }
            ' '           -> if (ignoreSpace) ignoreSpace = false else append(it)
            '='           -> append(" = ")
            else          -> append(it)
        }
    }
}

@doanvu2000

Copy link
Copy Markdown

công đức vô lượng

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment