Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add JSONP support to the JsonSupport trait.

  • Loading branch information...
commit 4489f04b0fd8ba7cf95b6aec43e51d329c32d53e 1 parent 6297163
Phil Wills authored
18 lift-json/src/main/scala/org/scalatra/liftjson/JsonSupport.scala
View
@@ -9,6 +9,15 @@ import net.liftweb.json.JsonAST.JValue
*/
trait JsonSupport extends ScalatraKernel {
+ /**
+ * If a request is made with a parameter in jsonpCallbackParameterNames it will
+ * be assumed that it is a JSONP request and the json will be returned as the
+ * argument to a function with the name specified in the corresponding parameter.
+ *
+ * By default no parameterNames will be checked
+ */
+ def jsonpCallbackParameterNames: Iterable[String] = None
+
override protected def contentTypeInferrer = ({
case _ : JValue => "application/json; charset=utf-8"
}: ContentTypeInferrer) orElse super.contentTypeInferrer
@@ -16,8 +25,13 @@ trait JsonSupport extends ScalatraKernel {
override protected def renderPipeline = ({
case jv : JValue =>
import net.liftweb._
- json.compact(json.render(jv)).getBytes("UTF-8")
- }: RenderPipeline) orElse super.renderPipeline
+ val jsonString = json.compact(json.render(jv))
+ val jsonWithCallback = for {
+ paramName <- jsonpCallbackParameterNames
+ callback <- params.get(paramName)
+ } yield "%s(%s);" format (callback, jsonString)
+ (jsonWithCallback.headOption.getOrElse(jsonString)).getBytes("UTF-8")
+ }: RenderPipeline) orElse super.renderPipeline
}
28 lift-json/src/test/scala/org/scalatra/liftjson/JsonSupportTest.scala
View
@@ -4,8 +4,8 @@ import org.scalatra.test.scalatest.ScalatraFunSuite
import org.scalatra.ScalatraServlet
class JsonSupportTest extends ScalatraFunSuite {
- val servletHolder = addServlet(classOf[JsonSupportTestServlet], "/*")
- servletHolder.setInitOrder(1) // force load on startup
+ addServlet(classOf[JsonSupportTestServlet], "/*")
+ addServlet(classOf[JsonPTestServlet], "/p/*")
test("JSON support test") {
get("/json") {
@@ -13,6 +13,20 @@ class JsonSupportTest extends ScalatraFunSuite {
response.body should equal ("""{"k1":"v1","k2":"v2"}""")
}
}
+
+ test("JSONP callback test with no callback name specified") {
+ get("/json", "callback" -> "function") {
+ response.mediaType should equal (Some("application/json"))
+ response.body should equal ("""{"k1":"v1","k2":"v2"}""")
+ }
+ }
+
+ test("JSONP callback test with callback name specified") {
+ get("/p/jsonp", "callback" -> "function") {
+ response.mediaType should equal (Some("application/json"))
+ response.body should equal ("""function({"k1":"v1","k2":"v2"});""")
+ }
+ }
}
@@ -23,3 +37,13 @@ class JsonSupportTestServlet extends ScalatraServlet with JsonSupport {
("k2" -> "v2")
}
}
+
+class JsonPTestServlet extends ScalatraServlet with JsonSupport {
+ override def jsonpCallbackParameterNames = Some("callback")
+
+ get("/jsonp") {
+ import net.liftweb.json.JsonDSL._
+ ("k1" -> "v1") ~
+ ("k2" -> "v2")
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.