Pretty-Print a JsonElement of kotlinx

152 views Asked by At

I have a kotlinx JsonElement at hand, and I want to pretty-print it.

How do I do that? The only methods I see for pretty-print need a data class, but my element does not fit any specific class.

//EDIT: added some code example

fun print(json: JsonObject) : String {
    val withVersion = jsonObject + ("Version" to 1.1)
    return withVersion.toString()  // returns wrong json
} 
1

There are 1 answers

0
ModdyFire On

I figured it out. I feel quite stupid. encodeToString works correctly even if the object is JsonElement.

Simply

Json { prettyPrint = true }.encodeToString(withVersion)