Skip to content

Instantly share code, notes, and snippets.

@kahneraja
Last active December 20, 2017 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kahneraja/0caeb89486c9cc33ddcab225b5e9c01f to your computer and use it in GitHub Desktop.
Save kahneraja/0caeb89486c9cc33ddcab225b5e9c01f to your computer and use it in GitHub Desktop.
Scala Worksheet: How to inject an implicit value into a Play json serializer...?
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
case class GlobalConfig(defaultAge: Int)
val config = GlobalConfig(34)
case class Person(name: String, age: Int)
object Person {
implicit val reads: Reads[Person] = (
(JsPath \ "name").read[String] and
(JsPath \ "age").readNullableWithDefault(????)[Int] // default age with some sort of di...
) (Person.apply _)
}
val json: JsValue = Json.parse("""
{
"name" : "John",
"age": 23
}
""")
val result: JsResult[Person] = (json).validate[Person]
result.get
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment