Skip to content

Instantly share code, notes, and snippets.

@EECOLOR
Created October 26, 2013 16:26
Show Gist options
  • Save EECOLOR/7171474 to your computer and use it in GitHub Desktop.
Save EECOLOR/7171474 to your computer and use it in GitHub Desktop.
Prints Json litterals for given JsValue
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