diff options
author | Mathias <mathias@spray.cc> | 2011-06-03 15:30:21 +0200 |
---|---|---|
committer | Mathias <mathias@spray.cc> | 2011-06-03 15:30:21 +0200 |
commit | 4d56f2e56694f7033d57752b1a61b73137058fcc (patch) | |
tree | bc166f1948b08cc9d996a4c7712f9e2eb4ee2b0e | |
parent | dd6eb57f6254923ae1c2e1ba79a79dcf1873e2bd (diff) | |
download | spray-json-4d56f2e56694f7033d57752b1a61b73137058fcc.tar.gz spray-json-4d56f2e56694f7033d57752b1a61b73137058fcc.tar.bz2 spray-json-4d56f2e56694f7033d57752b1a61b73137058fcc.zip |
Add JSONP support to JsonPrinters
-rw-r--r-- | src/main/scala/cc/spray/json/JsonPrinter.scala | 15 | ||||
-rw-r--r-- | src/test/scala/cc/spray/json/CompactPrinterSpec.scala | 3 |
2 files changed, 16 insertions, 2 deletions
diff --git a/src/main/scala/cc/spray/json/JsonPrinter.scala b/src/main/scala/cc/spray/json/JsonPrinter.scala index 6f1b20a..44ee9de 100644 --- a/src/main/scala/cc/spray/json/JsonPrinter.scala +++ b/src/main/scala/cc/spray/json/JsonPrinter.scala @@ -24,9 +24,20 @@ import java.lang.StringBuilder */ trait JsonPrinter extends (JsValue => String) { - def apply(x: JsValue) = { + def apply(x: JsValue): String = apply(x, None) + + def apply(x: JsValue, jsonpCallback: String): String = apply(x, Some(jsonpCallback)) + + def apply(x: JsValue, jsonpCallback: Option[String]): String = { val sb = new StringBuilder - print(x, sb) + jsonpCallback match { + case Some(callback) => { + sb.append(callback).append('(') + print(x, sb) + sb.append(')'); + } + case None => print(x, sb) + } sb.toString } diff --git a/src/test/scala/cc/spray/json/CompactPrinterSpec.scala b/src/test/scala/cc/spray/json/CompactPrinterSpec.scala index a517cd1..4ca4aac 100644 --- a/src/test/scala/cc/spray/json/CompactPrinterSpec.scala +++ b/src/test/scala/cc/spray/json/CompactPrinterSpec.scala @@ -43,6 +43,9 @@ class CompactPrinterSpec extends Specification { CompactPrinter(JsArray(JsNull, JsNumber(1.23), JsObject(JsField("key", true)))) mustEqual """[null,1.23,{"key":true}]""" ) + "properly print a JSON padding (JSONP) if requested" in { + CompactPrinter(JsTrue, Some("customCallback")) mustEqual("customCallback(true)") + } } }
\ No newline at end of file |