Last active
December 20, 2017 19:23
-
-
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...?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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