本文整理汇总了Scala中play.api.libs.json.JsDefined类的典型用法代码示例。如果您正苦于以下问题:Scala JsDefined类的具体用法?Scala JsDefined怎么用?Scala JsDefined使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JsDefined类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: WeatherForecastController
//设置package包名称以及导入依赖的类
package controllers
import play.api.mvc._
import scala.concurrent._
import javax.inject.{Inject, Singleton}
import play.api.libs.json.JsDefined
import services.WeatherForecastService
@Singleton
class WeatherForecastController @Inject() (
cc: ControllerComponents,
weatherForecastService: WeatherForecastService
)(implicit ec: ExecutionContext) extends AbstractController(cc) {
def getRawForecastForCity() = Action.async { implicit request =>
val cityName = request.getQueryString("city").mkString
weatherForecastService.getForecastByCityName(cityName).map(resp => Ok(resp.body))
}
def getForecastForCity() = Action.async { implicit request =>
val cityID = request.getQueryString("id").mkString
weatherForecastService.getForecastForCityByID(cityID).map(resp => {
val err = resp \ "error"
err match {
case err: JsDefined => BadRequest(resp)
case _ => Ok(resp)
}
})
}
}
示例2: SubscriptionController
//设置package包名称以及导入依赖的类
package com.clemble.loveit.user.controller
import javax.inject.{Inject, Singleton}
import com.clemble.loveit.user.service.SubscriptionManager
import play.api.libs.json.{JsBoolean, JsDefined, JsObject, JsString}
import play.api.mvc.{Action, Controller}
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class SubscriptionController @Inject() (subManager: SubscriptionManager, implicit val ec: ExecutionContext) extends Controller {
def subscribeCreator() = Action.async(parse.json[JsObject].map(_ \ "email"))(implicit req => {
req.body match {
case JsDefined(JsString(email)) =>
subManager.
subscribeCreator(email).
map(res => Ok(JsBoolean(res)))
case _ =>
Future.successful(NotFound)
}
})
def subscribeContributor() = Action.async(parse.json[JsObject].map(_ \ "email"))(implicit req => {
req.body match {
case JsDefined(JsString(email)) =>
subManager.
subscribeContributor(email).
map(res => Ok(JsBoolean(res)))
case _ =>
Future.successful(NotFound)
}
})
}
示例3: SignOutControllerSpec
//设置package包名称以及导入依赖的类
package controllers
import helpers.PlogSpec
import models.User
import org.scalatest.BeforeAndAfterEach
import org.scalatest.concurrent.ScalaFutures
import org.scalatestplus.play.PlaySpec
import play.api.libs.json.{JsDefined, JsObject, JsString, Json}
import play.api.test.FakeRequest
import play.api.test.Helpers._
import utils.auth.DefaultEnv
import com.mohiva.play.silhouette.test._
class SignOutControllerSpec extends PlaySpec with PlogSpec with BeforeAndAfterEach with ScalaFutures {
import models.JsonFormats._
val newUser = User(
None,
name = Some("foobar"),
email = "[email protected]",
password = "passw0rd",
activated = true
)
"SignOutController" should {
"sign out user" in {
// create user first
val content = contentAsJson(route(app, FakeRequest(POST, "/sign-up").withJsonBody(Json.toJson(newUser))).get)
val userId = (content.as[JsObject] \ "data" \ "userId").get.as[Long]
route(app, FakeRequest(POST, "/sign-in").withJsonBody(Json.toJson(newUser.copy(userId = Some(userId)))))
val resp = contentAsJson(route(app, FakeRequest(POST, "/sign-out").withAuthenticator[DefaultEnv](barUser.loginInfo)).get)
(resp \ "data") mustBe JsDefined(JsString("User signed out"))
}
}
}
示例4: FieldExtractor
//设置package包名称以及导入依赖的类
package controllers
import java.util.{Date, UUID}
import models.adverts.{Fields, Fuel}
import play.api.libs.json.{JsDefined, JsNull, JsValue}
// TODO: Do not use "get" it's not safe
object FieldExtractor {
def getUUID(json: JsValue) = {
(json \ Fields.id).get.as[UUID]
}
def getTitle(json: JsValue) = {
(json \ Fields.title).get.as[String]
}
def getPrice(json: JsValue) = {
(json \ Fields.price).get.as[Int]
}
def getIsNew(json: JsValue) = {
(json \ Fields.isNew).get.as[Boolean]
}
def getFuel(json: JsValue) = {
val name = (json \ Fields.fuel).getOrElse(JsNull).as[String]
Fuel.findByName(name)
}
def getMileage(json: JsValue): Option[Int] = {
json \ Fields.mileage match {
case JsDefined(value) => Some(value.as[Int])
case _ => None
}
}
def getFirstRegistration(json: JsValue): Option[Date] = {
json \ Fields.firstRegistration match {
case JsDefined(value) => Some(value.as[Date])
case _ => None
}
}
}
示例5: ApplicationControllerTest
//设置package包名称以及导入依赖的类
package dcos.metronome.api.v1.controllers
import dcos.metronome.api.{ MockApiComponents, OneAppPerTestWithComponents }
import org.scalatestplus.play.PlaySpec
import play.api.ApplicationLoader.Context
import play.api.libs.json.{ JsString, JsDefined }
import play.api.test.FakeRequest
import play.api.test.Helpers._
class ApplicationControllerTest extends PlaySpec with OneAppPerTestWithComponents[MockApiComponents] {
"ping" should {
"send a pong" in {
val ping = route(app, FakeRequest(GET, "/ping")).get
status(ping) mustBe OK
contentType(ping) mustBe Some("text/plain")
contentAsString(ping) must include("pong")
}
}
"metrics" should {
"give metrics as json" in {
val metrics = route(app, FakeRequest(GET, "/v1/metrics")).get
status(metrics) mustBe OK
contentType(metrics) mustBe Some("application/json")
contentAsJson(metrics) \ "version" mustBe JsDefined(JsString("3.0.0"))
}
}
override def createComponents(context: Context): MockApiComponents = new MockApiComponents(context)
}