Next is the trait that allows Simplified Json syntax :
Creates a Format[T] by resolving case class fields & required implicits at COMPILE-time
Creates a Format[T] by resolving case class fields & required implicits at COMPILE-time
If any missing implicit is discovered, compiler will break with corresponding error.
import play.api.libs.json.Json case class User(name: String, age: Int) implicit val userWrites = Json.format[User] // macro-compiler replaces Json.format[User] by injecting into compile chain // the exact code you would write yourself. This is strictly equivalent to: implicit val userWrites = ( (__ \ 'name).format[String] and (__ \ 'age).format[Int] )(User.apply, unlift(User.unapply))
Transform a stream of JsValue to a stream of A, keeping only successful results
Transform a stream of JsValue to a stream of A, keeping only successful results
val jsonStream: Enumerator[JsValue] = ??? val fooStream: Enumerator[Foo] = jsonStream &> Json.fromJson
Provided a Reads implicit for that type is available, convert a JsValue to any type.
Provided a Reads implicit for that type is available, convert a JsValue to any type.
Json value to transform as an instance of T.
Parse a byte array representing a json, and return it as a JsValue.
Parse a byte array representing a json, and return it as a JsValue.
The character encoding used will be automatically detected as UTF-8, UTF-16 or UTF-32, as per the heuristics in RFC-4627.
a byte array to parse
the JsValue representing the byte array
Parse a String representing a json, and return it as a JsValue.
Parse a String representing a json, and return it as a JsValue.
a String to parse
the JsValue representing the string
Convert a JsValue to its pretty string representation using default Jackson pretty printer (line feeds after each fields and 2-spaces indentation).
Convert a JsValue to its pretty string representation using default Jackson pretty printer (line feeds after each fields and 2-spaces indentation).
scala> Json.stringify(Json.obj( "field1" -> Json.obj( "field11" -> "value11", "field12" -> Json.arr("alpha", 123L) ) )) res0: String = {"field1":{"field11":"value11","field12":["alpha",123]}} scala> Json.prettyPrint(res0) res1: String = { "field1" : { "field11" : "value11", "field12" : [ "alpha", 123 ] } }
the JsValue to convert
a String with the json representation
Creates a Reads[T] by resolving case class fields & required implcits at COMPILE-time.
Creates a Reads[T] by resolving case class fields & required implcits at COMPILE-time.
If any missing implicit is discovered, compiler will break with corresponding error.
import play.api.libs.json.Json case class User(name: String, age: Int) implicit val userReads = Json.reads[User] // macro-compiler replaces Json.reads[User] by injecting into compile chain // the exact code you would write yourself. This is strictly equivalent to: implicit val userReads = ( (__ \ 'name).read[String] and (__ \ 'age).read[Int] )(User)
Convert a JsValue to its string representation.
Convert a JsValue to its string representation.
scala> Json.stringify(Json.obj( "field1" -> Json.obj( "field11" -> "value11", "field12" -> Json.arr("alpha", 123L) ) )) res0: String = {"field1":{"field11":"value11","field12":["alpha",123]}} scala> Json.stringify(res0) res1: String = {"field1":{"field11":"value11","field12":["alpha",123]}}
the JsValue to convert
a String with the json representation
Transform a stream of A to a stream of JsValue
Transform a stream of A to a stream of JsValue
val fooStream: Enumerator[Foo] = ??? val jsonStream: Enumerator[JsValue] = fooStream &> Json.toJson
Provided a Writes implicit for its type is available, convert any object into a JsValue.
Provided a Writes implicit for its type is available, convert any object into a JsValue.
Value to convert in Json.
Creates a Writes[T] by resolving case class fields & required implcits at COMPILE-time
Creates a Writes[T] by resolving case class fields & required implcits at COMPILE-time
If any missing implicit is discovered, compiler will break with corresponding error.
import play.api.libs.json.Json case class User(name: String, age: Int) implicit val userWrites = Json.writes[User] // macro-compiler replaces Json.writes[User] by injecting into compile chain // the exact code you would write yourself. This is strictly equivalent to: implicit val userWrites = ( (__ \ 'name).write[String] and (__ \ 'age).write[Int] )(unlift(User.unapply))
Helper functions to handle JsValues.