scalajson4s

How do I generate pretty JSON with json4s?


This snippet of code works very well, but it generates compact JSON (no line breaks / not very human readable).

import org.json4s.native.Serialization.write
implicit val jsonFormats = DefaultFormats

//snapshotList is a case class
val jsonString: String = write(snapshotList)

Is there an easy way to generate pretty JSON from this?

I have this workaround but I'm wondering if a more efficient way exists:

import org.json4s.jackson.JsonMethods._
val prettyJsonString = pretty(render(parse(jsonString)))

Solution

  • import org.json4s.jackson.Serialization.writePretty
    
    val jsonString: String = writePretty(snapshotList)