reformatJson

fun String.reformatJson(prettyPrint: Boolean = true): String

Parses this String as JSON and re-encodes it. Extension function on String.

The output is always valid JSON, which has no way to write a non-finite number. parseJson accepts NaN, Infinity and -Infinity, and a number too large for a Double, such as 1e500, which re-encodes as an infinity, but the re-encoding rejects all of them: JsonContentUtils.prettyFormat and JsonContentUtils.rawFormat keep kotlinx's default allowSpecialFloatingPointValues = false. JsonElement.toString writes such values back as they were read.

Return

the re-encoded JSON string

Parameters

prettyPrint

if true (the default), formats the output with indentation; otherwise outputs compact JSON

Throws

SerializationException

if this string is not valid JSON, as for parseJson, or if it holds NaN, Infinity, -Infinity or a number too large for a Double