Object

play.api.libs.json

Json

Related Doc: package json

Permalink

object Json

Helper functions to handle JsValues.

Source
Json.scala
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Json
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Type Members

  1. sealed trait JsValueWrapper extends AnyRef

    Permalink

    Next is the trait that allows Simplified Json syntax :

    Next is the trait that allows Simplified Json syntax :

    Example :

    JsObject(Seq(
       "key1", JsString("value"),
       "key2" -> JsNumber(123),
       "key3" -> JsObject(Seq("key31" -> JsString("value31")))
    )) == Json.obj( "key1" -> "value", "key2" -> 123, "key3" -> obj("key31" -> "value31"))
    
    JsArray(JsString("value"), JsNumber(123), JsBoolean(true)) == Json.arr( "value", 123, true )

    There is an implicit conversion from any Type with a Json Writes to JsValueWrapper which is an empty trait that shouldn't end into unexpected implicit conversions.

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def arr(fields: JsValueWrapper*): JsArray

    Permalink
  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def asciiStringify(json: JsValue): String

    Permalink

    Convert a JsValue to its string representation, escaping all non-ascii characters using \uXXXX syntax.

    Convert a JsValue to its string representation, escaping all non-ascii characters using \uXXXX syntax.

    This is particularly useful when the output JSON will be executed as javascript, since JSON is not a strict subset of javascript (see JSON: The JavaScript subset that isn't).

    scala> Json.asciiStringify(JsString("some\u2028text\u2029"))
    res0: String = "some\u2028text\u2029"
    
    scala> Json.stringify(JsString("some\u2028text\u2029"))
    res1: String = "sometext"
    json

    the JsValue to convert

    returns

    a String with the json representation with all non-ascii characters escaped.

  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. macro def format[A]: OFormat[A]

    Permalink

    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))
  12. def fromJson[T](json: JsValue)(implicit fjs: Reads[T]): JsResult[T]

    Permalink

    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

    Json value to transform as an instance of T.

  13. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  14. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  15. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  16. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  17. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  18. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  19. def obj(fields: (String, JsValueWrapper)*): JsObject

    Permalink
  20. def parse(input: Array[Byte]): JsValue

    Permalink

    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.

    input

    a byte array to parse

    returns

    the JsValue representing the byte array

  21. def parse(input: InputStream): JsValue

    Permalink

    Parse an InputStream representing a json, and return it as a JsValue.

    Parse an InputStream representing a json, and return it as a JsValue.

    input

    as InputStream to parse

    returns

    the JsValue representing the InputStream

  22. def parse(input: String): JsValue

    Permalink

    Parse a String representing a json, and return it as a JsValue.

    Parse a String representing a json, and return it as a JsValue.

    input

    a String to parse

    returns

    the JsValue representing the string

  23. def prettyPrint(json: JsValue): String

    Permalink

    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 ]
      }
    }
    json

    the JsValue to convert

    returns

    a String with the json representation

  24. macro def reads[A]: Reads[A]

    Permalink

    Creates a Reads[T] by resolving case class fields & required implicits at COMPILE-time.

    Creates a Reads[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 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)
  25. def stringify(json: JsValue): String

    Permalink

    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]}}
    json

    the JsValue to convert

    returns

    a String with the json representation

  26. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  27. implicit def toJsFieldJsValueWrapper[T](field: T)(implicit w: Writes[T]): JsValueWrapper

    Permalink
  28. def toJson[T](o: T)(implicit tjs: Writes[T]): JsValue

    Permalink

    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.

    o

    Value to convert in Json.

  29. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  30. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  31. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  32. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  33. macro def writes[A]: OWrites[A]

    Permalink

    Creates a Writes[T] by resolving case class fields & required implicits at COMPILE-time

    Creates a Writes[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.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))

Deprecated Value Members

  1. def fromJson[A](implicit arg0: Reads[A]): Enumeratee[JsValue, A]

    Permalink

    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
    Annotations
    @deprecated
    Deprecated

    (Since version 2.5.0) Use Enumeratee.map[JsValue]((json: JsValue) => Json.fromJson(json)) ><> Enumeratee.collect[JsResult[A]] { case JsSuccess(value, _) => value } instead

  2. def toJson[A](implicit arg0: Writes[A]): Enumeratee[A, JsValue]

    Permalink

    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
    Annotations
    @deprecated
    Deprecated

    (Since version 2.5.0) Use Enumeratee.map[A](Json.toJson(_)) instead

Inherited from AnyRef

Inherited from Any

Ungrouped