Json

play.api.libs.json.Json
object Json extends JsonFacade

Helper functions to handle JsValues.

Attributes

Graph
Supertypes
trait JsonFacade
class Object
trait Matchable
class Any
Self type
Json.type

Members list

Type members

Classlikes

Flag to indicate the macros can use the type default values (e.g. default values for the case class parameters) when applicable.

Flag to indicate the macros can use the type default values (e.g. default values for the case class parameters) when applicable.

import play.api.libs.json._, Json._

type Opts = MacroOptions with DefaultValues

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
sealed trait JsValueWrapper

Next is the trait that allows Simplified Json syntax :

Next is the trait that allows Simplified Json syntax :

Example:

import play.api.libs.json._

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

JsArray(Seq(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.

Attributes

Supertypes
class Object
trait Matchable
class Any
object MacroOptions

Attributes

Companion
trait
Supertypes
class Object
trait Matchable
class Any
Self type
sealed trait MacroOptions

Compile-time base options for macro usage.

Compile-time base options for macro usage.

import play.api.libs.json.Json

case class Foo(v: String)

Json.using[Json.MacroOptions].format[Foo]
// equivalent to Json.format[Foo]

Attributes

Companion
object
Supertypes
class Object
trait Matchable
class Any
final class WithOptions[Opts <: MacroOptions](val config: Aux[Opts]) extends JsonFacade, JsMacrosWithOptions[Opts]

JSON facade with some macro options.

JSON facade with some macro options.

Type parameters

Opts

the compile-time options

Attributes

Supertypes
trait JsMacrosWithOptions[Opts]
trait JsonFacade
class Object
trait Matchable
class Any

Inherited classlikes

final class Placeholder

Only for internal purposes

Only for internal purposes

Attributes

Inherited from:
JsValueMacros (hidden)
Supertypes
class Object
trait Matchable
class Any
object Placeholder

Only for internal purposes

Only for internal purposes

Attributes

Inherited from:
JsValueMacros (hidden)
Supertypes
class Object
trait Matchable
class Any

Types

Alias for MacroOptions with DefaultValues

Alias for MacroOptions with DefaultValues

import play.api.libs.json.Json

Json.using[Json.WithDefaultValues]

Attributes

Value members

Concrete methods

def arr(items: JsValueWrapper*): JsArray

Returns a JsArray with given items.

Returns a JsArray with given items.

Attributes

def asciiStringify(json: JsValue): String

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

Converts a JsValue to its string representation, escaping all non-ascii characters using \u005CuXXXX 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).

import play.api.libs.json.{ Json, JsString }

Json.asciiStringify(JsString("some\\u005Ctext\\u005C"))
// => "some\\u005Ctext\\u005C"

Json.stringify(JsString("some\\u005Ctext\\u005C"))
// => "sometext"

$jsonParam $returnStringRepr with all non-ascii characters escaped.

Attributes

def configured[Opts <: MacroOptions](implicit config: Aux[Opts]): WithOptions[Opts]

Returns a JsonFacade using the current JSON configuration.

Returns a JsonFacade using the current JSON configuration.

Type parameters

C

the type of compile-time configuration

import play.api.libs.json.{ Json, Reads }
case class Foo(v: String)
// Materializes a `Reads[Foo]`,
// with the configuration resolved at compile time
val r: Reads[Foo] = Json.configured.reads[Foo]

Attributes

def formatEnum[E <: Enumeration](enum: E): Format[Value]

Creates a Format[E] by automatically creating Reads[E] and Writes[E] for any Enumeration E

Creates a Format[E] by automatically creating Reads[E] and Writes[E] for any Enumeration E

import play.api.libs.json.{ Format, Json }

object DayOfWeek extends Enumeration {

type DayOfWeek = Value

val Mon = Value("Monday")
val Tue = Value("Tuesday")
val Wed = Value("Wednesday")
// etc.

 implicit val format1: Format[DayOfWeek] = Json.formatEnum(DayOfWeek)
 // or 'this' if defining directly in Enum
 implicit val format2: Format[DayOfWeek] = Json.formatEnum(this)
}

Json.toJson(Mon) will produce "Monday".

Type parameters

E

type of Enum

Value parameters

enum

Enumeration object

Attributes

def fromJson[T](json: JsValue)(implicit fjs: Reads[T]): JsResult[T]

Converts a JsValue to a value of requested type T.

Converts a JsValue to a value of requested type T.

Type parameters

T

The type of conversion result, only supported if a Reads implicit is available for. $jsonParam

Attributes

def newBuilder: Builder[(String, JsValueWrapper), JsObject]

Returns a JSON object builder.

Returns a JSON object builder.

 import play.api.libs.json.{ Json, JsObject }

 // Create a new builder
 val builder: JsObjectBuilder = JsObject.newBuilder

 // Add key-value pairs to the builder
 builder += ("name" -> "John Doe")
 builder += ("age" -> 25)

 // Clear the builder
 builder.clear()

 // Add more key-value pairs
 builder += ("email" -> "[email protected]")
 builder += ("address" -> "123 Street")

 // Build the final JsObject
 val result: JsObject = builder.result()

 // Print the resulting JsObject
 println(result)

This will output:

 {"email":"[email protected]","address":"123 Street"}

Attributes

def obj(fields: (String, JsValueWrapper)*): JsObject

Returns a JsObject with given fields.

Returns a JsObject with given fields.

Value parameters

fields

the object fields specified as pairs of name and value

Attributes

def parse(input: String): JsValue

Parses a String representing a JSON input, and returns it as a JsValue.

Parses a String representing a JSON input, and returns it as a JsValue.

Value parameters

input

the String to parse

Attributes

def parse(input: InputStream): JsValue

Parses a stream representing a JSON input, and returns it as a JsValue.

Parses a stream representing a JSON input, and returns it as a JsValue.

Value parameters

input

the InputStream to parse

Attributes

def parse(input: Array[Byte]): JsValue

Parses some bytes representing a JSON input, and returns it as a JsValue.

Parses some bytes representing a JSON input, and returns 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.

Value parameters

input

the byte array to parse

Attributes

def prettyPrint(json: JsValue): String

Converts a JsValue to its pretty string representation using default pretty printer (line feeds after each fields and 2-spaces indentation).

Converts a JsValue to its pretty string representation using default pretty printer (line feeds after each fields and 2-spaces indentation).

import play.api.libs.json.Json

val res0 = Json.obj(
 "field1" -> Json.obj(
   "field11" -> "value11",
   "field12" -> Json.arr("alpha", 123L)
 )
)
// => {"field1":{"field11":"value11","field12":["alpha",123]}}

Json.prettyPrint(res0)
// =>
// {
//   "field1" : {
//     "field11" : "value11",
//     "field12" : [ "alpha", 123 ]
//   }
// }

$jsonParam $returnStringRepr.

Attributes

def stringify(json: JsValue): String

Converts a JsValue to its string representation.

Converts a JsValue to its string representation.

import play.api.libs.json.Json

val input = Json.obj(
 "field1" -> Json.obj(
   "field11" -> "value11",
   "field12" -> Json.arr("alpha", 123L)
 )
)

Json.stringify(input)
// => {"field1":{"field11":"value11","field12":["alpha",123]}}

$jsonParam

Attributes

Returns

a String with the json representation

def toBytes(json: JsValue): Array[Byte]

Converts a JsValue to bytes (using UTF-8 encoding).

Converts a JsValue to bytes (using UTF-8 encoding).

$jsonParam

Attributes

Returns

an Array[Byte] representing the UTF-8-encoded JSON

def toJsObject[T](o: T)(implicit tjs: OWrites[T]): JsObject

Converts any object writeable value to a JsObject.

Converts any object writeable value to a JsObject.

A value is writeable as an object, if a OWrites implicit is available for its type.

Type parameters

T

the type of the value to be written as JsObject

Value parameters

o

the value to convert as JSON object

Attributes

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

Converts any writeable value to a JsValue.

Converts any writeable value to a JsValue.

A value is writeable if a Writes implicit is available for its type.

Type parameters

T

the type of the value to be written as JSON

Value parameters

o

the value to convert as JSON

Attributes

def using[Opts <: MacroOptions]: WithOptions[Opts]

Returns an inference context to call the JSON macros, using explicit compile-time options.

Returns an inference context to call the JSON macros, using explicit compile-time options.

Type parameters

Opts

the compile-time options

Attributes

Inherited methods

inline def format[A]: OFormat[A]

Creates a OFormat[T] by resolving, at compile-time, the case class fields or sealed family, and the required implicits.

Creates a OFormat[T] by resolving, at compile-time, the case class fields or sealed family, and the required implicits.

$macroWarning

$macroTypeParam

import play.api.libs.functional.syntax._
import play.api.libs.json.{ Json, JsonConfiguration, __ }

case class User(userName: String, age: Int)

val userFormat1 = 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:
val userFormat2 = (
  (__ \ implicitly[JsonConfiguration].naming("userName")).format[String] and
  (__ \ implicitly[JsonConfiguration].naming("age")).format[Int]
)(User.apply, unlift(User.unapply))

Attributes

Inherited from:
JsMacros (hidden)
inline def reads[A]: Reads[A]

Creates a Reads[T] by resolving, at compile-time, the case class fields or sealed family, and the required implicits.

Creates a Reads[T] by resolving, at compile-time, the case class fields or sealed family, and the required implicits.

$macroWarning

$macroTypeParam

import play.api.libs.json.{ Json, Reads }

case class User(userName: String, age: Int)

implicit val userReads: Reads[User] =
 Json.using[Json.MacroOptions with Json.DefaultValues].reads[User]

Attributes

Inherited from:
JsMacros (hidden)
inline def valueFormat[A <: AnyVal]: Format[A]

Creates a OFormat[T] by resolving, if T is a ValueClass (see valueReads and valueWrites).

Creates a OFormat[T] by resolving, if T is a ValueClass (see valueReads and valueWrites).

$macroWarning

$macroTypeParam

import play.api.libs.json.{ Format, Json }

final class User(val name: String) extends AnyVal

implicit val userFormat: Format[User] = Json.valueFormat[User]

Attributes

Inherited from:
JsValueMacros (hidden)
inline def valueReads[A <: AnyVal]: Reads[A]

Creates a Reads[A], if A is a ValueClass, by resolving at compile-time the Reads for the underlying type.

Creates a Reads[A], if A is a ValueClass, by resolving at compile-time the Reads for the underlying type.

$macroWarning

$macroTypeParam

import play.api.libs.json.{ Json, Reads }

final class IdText(val value: String) extends AnyVal

// Based on provided Reads[String] corresponding to `value: String`
val r: Reads[IdText] = Json.valueReads

Attributes

Inherited from:
JsValueMacros (hidden)
inline def valueWrites[A <: AnyVal]: Writes[A]

Creates a OWrites[T], if T is a ValueClass, by resolving at compile-time the Writes for the underlying type.

Creates a OWrites[T], if T is a ValueClass, by resolving at compile-time the Writes for the underlying type.

$macroWarning

$macroTypeParam

import play.api.libs.json.{ Json, Writes }

final class TextId(val value: String) extends AnyVal

// Based on provided Writes[String] corresponding to `value: String`
val w: Writes[TextId] = Json.valueWrites[TextId]

Attributes

Inherited from:
JsValueMacros (hidden)
inline def writes[A]: OWrites[A]

Creates a OWrites[T] by resolving, at compile-time, the case class fields or sealed family, and the required implicits.

Creates a OWrites[T] by resolving, at compile-time, the case class fields or sealed family, and the required implicits.

$macroWarning

$macroTypeParam

import play.api.libs.functional.syntax._
import play.api.libs.json.{ Json, JsonConfiguration, __ }

case class User(userName: String, age: Int)

implicit val userWrites1 = 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 userWrites2 = (
  (__ \ implicitly[JsonConfiguration].naming("userName")).write[String] and
  (__ \ implicitly[JsonConfiguration].naming("age")).write[Int]
)(unlift(User.unapply))

Attributes

Inherited from:
JsMacros (hidden)

Implicits

Implicits

implicit def toJsFieldJsValueWrapper[T](field: T)(implicit w: Writes[T]): JsValueWrapper