Reputation: 537
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)))
Upvotes: 7
Views: 6333
Reputation: 1380
import org.json4s.jackson.Serialization.writePretty
val jsonString: String = writePretty(snapshotList)
Upvotes: 16
Reputation: 21220
You can use the ObjectMapper writerWithDefaultPrettyPrinter
function:
ObjectMapper mapper = new ObjectMapper();
val pretty = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(jsonString));
This returns a ObjectWriter
object from which you can get a pretty-formatted string.
Upvotes: 0