Skip to content

Instantly share code, notes, and snippets.

@aloiscochard
Created October 11, 2011 18:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloiscochard/1278959 to your computer and use it in GitHub Desktop.
Save aloiscochard/1278959 to your computer and use it in GitHub Desktop.
Scala JSON Pretty Formatter
import scala.util.parsing.json._
val prettyFormatter: JSONFormat.ValueFormatter = {
def create(level: Int): JSONFormat.ValueFormatter = (x: Any) => {
x match {
case s: String => "\"" + JSONFormat.quoteString(s) + "\""
case jo: JSONObject =>
"{\n" +
"\t" * (level + 1) +
jo.obj.map({
case (k, v) => JSONFormat.defaultFormatter(k.toString) + ": " + create(level + 1)(v)
}).mkString(",\n" +
"\t" * (level + 1)) +
"\n" +
"\t" * level +
"}"
case ja: JSONArray => ja.toString(create(level))
case other => other.toString
}
}
create(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment