Created
October 11, 2011 18:35
-
-
Save aloiscochard/1278959 to your computer and use it in GitHub Desktop.
Scala JSON Pretty Formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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