Created
October 26, 2013 16:26
-
-
Save EECOLOR/7171474 to your computer and use it in GitHub Desktop.
Prints Json litterals for given JsValue
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
def print(json: JsValue): String = { | |
json match { | |
case JsObject(values) => | |
val contents = | |
values.map { | |
case (k, v) => s""""$k" -> ${print(v)}""" | |
}.mkString(", ") | |
s"Json.obj($contents)" | |
case JsArray(values) => | |
val contents = values.map(print).mkString(", ") | |
s"Json.arr($contents)" | |
case JsString(value) => | |
"\"" + value.replaceAll("\"", """\\"""").replaceAll("""\\n""", """\\\\n""") + "\"" | |
case JsBoolean(value) => value.toString | |
case JsNumber(value) => | |
val suffix = if (value.isValidInt) "" else "L" | |
value.toString + suffix | |
case JsNull => "JsNull" | |
case j: JsValue => j.as[String] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment