Marcin Wisniowski
09/25/2024, 11:17 AMJsonParser.parse
to parse any JSON and then gson.toJson(parsed)
, to print, with the GSON instance using pretty printing.
In kotlinx.serialisation, we can also parse arbitrary JSON with json.parseToJsonElement
, and then print it with JsonElement.toString()
. But the printing doesn't use the Json
instance and so doesn't use the pretty-printing setting that is enabled on it.Vampire
09/25/2024, 11:26 AMval json = Json { prettyPrint = true }
val prettyPrinted = json.encodeToString(json.parseToJsonElement("{ my: json }"))
Marcin Wisniowski
09/25/2024, 11:27 AMJsonElement
can be passed to encodeToString
, I didn't realize that.Vampire
09/25/2024, 11:27 AMMarcin Wisniowski
09/25/2024, 11:30 AMfarmerbb
10/14/2024, 10:21 PMkotlinx.serialization.encodeToString
in order for this to work 👍