Skip to content

Instantly share code, notes, and snippets.

@tdrozdowski
Last active March 23, 2016 09:15
Show Gist options
  • Save tdrozdowski/7348647 to your computer and use it in GitHub Desktop.
Save tdrozdowski/7348647 to your computer and use it in GitHub Desktop.
Various code snippets for working with the Play Framework
import play.api.libs.json._
import play.api.libs.json.Reads._
// construct a json
val user = Json.obj("email" -> "[email protected]", "firstName" -> "Foo", "lastName" -> "Bar")
// > user: play.api.libs.json.JsObject = {"email":"[email protected]","firstName":"Foo","lastName":"Bar"}
println(s"user -> $user")
// > user -> {"email":"[email protected]","firstName":"Foo","lastName":"Bar"}
// get just the email from the JsValue
val email = (user \ "email").asOpt[String].getOrElse("No email!")
// > email: String = [email protected]
println(s"email -> $email")
// > email -> [email protected]
// create a case class to serialize to
case class User(email : String, firstName : String, lastName :String, middleName : Option[String])
// define a formats using JSON 'inception'
implicit val userFormats = Json.format[User]
// ask for an instance of the case class
val userFromJson = Json.fromJson(user)
// > userFromJson: play.api.libs.json.JsResult[User] = JsSuccess(User([email protected],Foo,Bar,None),)
println(s"user class -> $userFromJson")
// > user class -> JsSuccess(User([email protected],Foo,Bar,None),)
// add the middleName - via concatenation
val userWithMiddleName = user ++ Json.obj("middleName" -> "Blazz")
println(s"userWithMiddleName -> $userWithMiddleName")
// > userWithMiddleName -> {"email":"[email protected]","firstName":"Foo","lastName":"Bar", "middleName":"Blazz"}
// update the middleName - via transform
def updateMiddleName(name : String) = {
(__ \ 'middleName).json.update(
of[JsString].map { case JsString(_) => JsString(name) }
)
}
val userUpdatedMiddleName = userWithMiddleName.transform(updateMiddleName("Blee"))
// > userUpdatedMiddleName: play.api.libs.json.JsResult[play.api.libs.json.JsObject]
// = JsSuccess({"email":"[email protected]","firstName":"Foo","lastName":"Bar","middlename":"Blee"},/middleName)
println(s"updated middleName ${userUpdatedMiddleName.get}")
// > updated middleName {"email":"[email protected]","firstName":"Foo","lastName":"Bar","middleName":"Blee"}
import play.api.libs.ws.WS
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Try, Success}
val futureResults = WS.url("http://localhost:9000/ping").withHeaders("Accept" -> "application/json").get().map {
results =>
Try(
if (results.status == 200)
results.json
else
throw new Exception(s"Error while requesting ping! Status: ${results.status} Body: ${results.body}")
)
}
// > futureResults: scala.concurrent.Future[scala.util.Try[play.api.libs.json.JsValue]]
futureResults.map {
case Success(json) => println(s"received JSON: $json")
case Failure(e) => println(s"Failure due to: ${e.getMessage}")
}
// received JSON: {"results":"ok"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment