play.api.libs

json

package json

Json API For example:

 case class User(id: Long, name: String, friends: List[User])

 implicit object UserFormat extends Format[User] {
  def reads(json: JsValue): User = User(
    (json \ "id").as[Long],
    (json \ "name").as[String],
    (json \ "friends").asOpt[List[User]].getOrElse(List()))
  def writes(u: User): JsValue = JsObject(List(
    "id" -> JsNumber(u.id),
    "name" -> JsString(u.name),
    "friends" -> JsArray(u.friends.map(fr => JsObject(List("id" -> JsNumber(fr.id),
    "name" -> JsString(fr.name)))))))
}

//then in a controller:
object MyController extends Controller {
   def displayUserAsJson(id: String) = Action {
      Ok(toJson( User(id.toLong, "myName", friends: List())))
   }
   def saveUser(jsonString: String)= Action {
     val user = play.api.libs.json.parse(jsonString).as[User]
     myDataStore.save(user)
     Ok
   }
}
Visibility
  1. Public
  2. All

Type Members

  1. trait DefaultFormat extends AnyRef

    Default Json formatters.

  2. trait DefaultReads extends AnyRef

    Default deserializer type classes.

  3. trait DefaultWrites extends AnyRef

    Default Serializers.

  4. trait Format [T] extends Writes[T] with Reads[T]

    Json formatter: write an implicit to define both a serializer and a deserializer for any type.

  5. case class JsArray (value: Seq[JsValue]) extends JsValue with Product with Serializable

    Represent a Json arayy value.

  6. case class JsBoolean (value: Boolean) extends JsValue with Product with Serializable

    Represent a Json boolean value.

  7. case class JsNumber (value: BigDecimal) extends JsValue with Product with Serializable

    Represent a Json number value.

  8. case class JsObject (fields: Seq[(String, JsValue)]) extends JsValue with Product with Serializable

    Represent a Json object value.

  9. case class JsString (value: String) extends JsValue with Product with Serializable

    Represent a Json string value.

  10. case class JsUndefined (error: String) extends JsValue with Product with Serializable

    Represent a missing Json value.

  11. trait JsValue extends AnyRef

    Generic json value

  12. trait Reads [T] extends AnyRef

    Json deserializer: write an implicit to define a deserializer for any type.

  13. trait Writes [T] extends AnyRef

    Json serializer: write an implicit to define a serializer for any type

Value Members

  1. object Format extends DefaultFormat

    Default Json formatters.

  2. object Generic extends AnyRef

    Generic Json formatters to handle Product types.

  3. object JsNull extends JsValue with Product with Serializable

    Represent a Json null value.

  4. object Json extends AnyRef

    Helper functions to handle JsValues.

  5. object Reads extends DefaultReads

    Default deserializer type classes.

  6. object Writes extends DefaultWrites

    Default Serializers.