Package

play.api

libs

Permalink

package libs

Contains various APIs that are useful while developing web applications.

Source
package.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. libs
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. case class Jsonp(padding: String, json: JsValue) extends Product with Serializable

    Permalink

    JSONP helper.

    JSONP helper.

    Example of use, provided the following route definition:

    GET  /my-service       Application.myService(callback: String)

    The following action definition:

    def myService(callback: String) = Action {
      val json = ...
      Ok(Jsonp(callback, json))
    }

    And the following request:

    GET /my-service?callback=foo

    The response will have content type “application/javascript” and will look like the following:

    foo({...});

    Another example, showing how to serve either JSON or JSONP from the same action, according to the presence of a “callback” parameter in the query string:

    def myService = Action { implicit request =>
      val json = ...
      request.queryString.get("callback").flatMap(_.headOption) match {
        case Some(callback) => Ok(Jsonp(callback, json))
        case None => Ok(json)
      }
    }

Value Members

  1. object Codecs

    Permalink

    Utilities for Codecs operations.

  2. object Collections

    Permalink

    Utilities functions for Collections

  3. object Comet

    Permalink

    Helper function to produce a Comet using Akka Streams.

    Helper function to produce a Comet using Akka Streams.

    Please see https://en.wikipedia.org/wiki/Comet_(programming) for details of Comet.

    Example:

    def streamClock() = Action {
      val df: DateTimeFormatter = DateTimeFormatter.ofPattern("HH mm ss")
      val tickSource = Source.tick(0 millis, 100 millis, "TICK")
      val source = tickSource.map((tick) => df.format(ZonedDateTime.now()))
      Ok.chunked(source via Comet.flow("parent.clockChanged"))
    }
  4. object EventSource

    Permalink

    This class provides an easy way to use Server Sent Events (SSE) as a chunked encoding, using an Akka Source.

    This class provides an easy way to use Server Sent Events (SSE) as a chunked encoding, using an Akka Source.

    Please see the Server-Sent Events specification for details.

    An example of how to display an event stream:

    import java.time.ZonedDateTime
    import java.time.format.DateTimeFormatter
    import javax.inject.Singleton
    import akka.stream.scaladsl.Source
    import play.api.http.ContentTypes
    import play.api.libs.EventSource
    import play.api.mvc._
    
    import scala.concurrent.duration._
    
    def liveClock() = Action {
      val df: DateTimeFormatter = DateTimeFormatter.ofPattern("HH mm ss")
      val tickSource = Source.tick(0 millis, 100 millis, "TICK")
      val source = tickSource.map { (tick) =>
        df.format(ZonedDateTime.now())
      }
      Ok.chunked(source via EventSource.flow).as(ContentTypes.EVENT_STREAM)
    }
  5. object Files

    Permalink

    FileSystem utilities.

  6. object JNDI

    Permalink

    JNDI Helpers.

  7. object Jsonp extends Serializable

    Permalink
  8. package concurrent

    Permalink
  9. package crypto

    Permalink
  10. package functional

    Permalink
  11. package json

    Permalink

    Json API For example:

    Json API For example:

    import play.api.libs.json._
    import play.api.libs.functional.syntax._
    
    case class User(id: Long, name: String, friends: Seq[User] = Seq.empty)
    object User {
    
      // In this format, an undefined friends property is mapped to an empty list
      implicit val format: Format[User] = (
        (__ \ "id").format[Long] and
        (__ \ "name").format[String] and
        (__ \ "friends").lazyFormatNullable(implicitly[Format[Seq[User]]])
          .inmap[Seq[User]](_ getOrElse Seq.empty, Some(_))
      )(User.apply, unlift(User.unapply))
    }
    
    //then in a controller:
    
    object MyController extends Controller {
       def displayUserAsJson(id: String) = Action {
          Ok(Json.toJson(User(id.toLong, "myName")))
       }
       def saveUser(jsonString: String)= Action {
         val user = Json.parse(jsonString).as[User]
         myDataStore.save(user)
         Ok
       }
    }
  12. package streams

    Permalink
  13. package typedmap

    Permalink
  14. package ws

    Permalink

    Provides implicit type classes when you import the package.

Inherited from AnyRef

Inherited from Any

Ungrouped