toJsonString

inline fun <reified T> T.toJsonString(prettyPrint: Boolean = true): String

Serializes this value to a JSON string.

Return

the JSON string representation

Parameters

prettyPrint

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

Type Parameters

T

the type to serialize (must be @Serializable or a JsonElement)

Throws

SerializationException

if the value holds a non-finite Double or Float (NaN or an infinity), which JSON cannot represent; see reformatJson


Deprecated

Passing an argument to toJsonString on a String serializes it as a JSON string literal. Use reformatJson.

Replace with

reformatJson()

Parses this String as JSON and re-encodes it as a pretty-printed JSON string. Extension function on String.