/play-json-api

JSON API in playframework

Primary LanguageScala

play-json-api

This project served to demonstrate the playframework JSON API in Java and in Scala.

It was used at the Berlin User Group.

  1. build JSON with Java

  2. build JSON with Scala

  3. consume JSON (request body) with Java

    in java:

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Json.setObjectMapper(mapper);

(not needed anymore in trunk: playframework/playframework#3670)

  1. consume JSON (request body) with Scala
  • validate id != -1 with (.filter(_ != -1))
  • show with optional field with Option[String]
  • defined default reads and writes
  • defined reads and writes in one time (with Formats)
  1. send and consume JSON with WS in Java

  2. send and consume JSON with WS in Scala

  3. consume and transform JSON in Scala

    val copyValue: Reads[JsObject] = (
      (__ \ "id").json.prune and
      (__ \ "name").json.copyFrom( (__ \ "group").json.pick )
    ).reduce

    val addValue: Reads[JsObject] = (
      (__ \ "id").json.prune and
      (__ \ "group").json.pickBranch and
      (__ \ "myfield").json.put(JsNumber(42))
    ).reduce