Skip to content

Latest commit

 

History

History
155 lines (108 loc) · 3.16 KB

README.play24.md

File metadata and controls

155 lines (108 loc) · 3.16 KB

play-json4s

This module allows you to use json4s in your play20 application.

Install

Current version is

  • 0.1.0 (for Play 2.1)
  • 0.2.0 (for Play 2.2)
  • 0.3.1 (for Play 2.3)
  • 0.4.2 (for Play 2.4)

Add the following lines in your build.sbt.

To use json4s-native

libraryDependencies ++= Seq(
  "com.github.tototoshi" %% "play-json4s-native" % "0.4.2",
  "com.github.tototoshi" %% "play-json4s-test-native" % "0.4.2" % "test"
)

To use json4s-jackson

libraryDependencies ++= Seq(
  "com.github.tototoshi" %% "play-json4s-jackson" % "0.4.2",
  "com.github.tototoshi" %% "play-json4s-test-jackson" % "0.4.2" % "test"
)

Usage

With controllers

To use json4s-native

import com.github.tototoshi.play2.json4s.native._

To use json4s-jackson

import com.github.tototoshi.play2.json4s.jackson._

All you have to do is to mix-in Json4s trait.

object Application extends Controller with Json4s {

Json4s provides

  • Body parser for Json4s
  • Implicit objects to create response objects with json Body

See below

import org.json4s._
import org.json4s.native.JsonMethods._
// or import org.json4s.jackson.JsonMethods._
case class Person(id: Long, name: String, age: Int)

object Application extends Controller with Json4s {

  implicit val formats = DefaultFormats

  def get = Action { implicit request =>
    Ok(Extraction.decompose(Person(1, "pamyupamyu", 20)))
  }

  def post = Action(json) { implicit request =>
    Ok(request.body.extract[Person].name)
  }

}

With WS

You can use json4s objects as request body.

WS.url("http://......")
  .post(Extraction.decompose(Person(1, "pamyupamyu", 20))),

With tests

This module also provides test helpers for Json4s.

To use test helpers for json4s-native

import com.github.tototoshi.play2.json4s.test.native.Helpers._

To use test helpers for json4s-jackson

import com.github.tototoshi.play2.json4s.test.jackson.Helpers._

This add the features as the following

  • FakeApplication#withJson4sBody
  • contentAsJson4s
(some imports as usual ...)

import org.json4s._
import org.json4s.native.JsonMethods._
import com.github.tototoshi.play2.json4s.test.native.Helpers._

class ApplicationSpec extends Specification {

  "Json4sPlayModule" should {

    "allow you to use json4s-native value as response" in {
      val res = Application.get(FakeRequest())
      contentType(res) must beSome("application/json")
      contentAsJson4s(res) must_== (JObject(List(("id",JInt(1)), ("name",JString("ぱみゅぱみゅ")), ("age",JInt(20)))))
    }

    "accept native json request" in {
      val fakeRequest = FakeRequest().withJson4sBody(parse("""{"id":1,"name":"ぱみゅぱみゅ","age":20}"""))
      val res = Application.post(fakeRequest)
      contentAsString(res) must beEqualTo ("ぱみゅぱみゅ")
    }

  }

}

ChangeLog

0.4.2

  • Updated json4s version to 3.3.0

0.4.1

  • Re-published 0.4.0 because of invalid sha1 problem of sonatype

0.4.0

  • Support Play 2.4

0.3.1

  • Added custom parse error handler

0.3.0

  • Support Play 2.3

License

Apache 2.0