I have a Scala case class
case class Example(name: String, number: Int)
and a companion object
object Example {
implicit object ExampleFormat extends Format[Example] {
def reads(json: JsValue) = {
JsSuccess(Example(
(json \ "name").as[String],
(json \ "number").as[Int]))
}
def writes(...){}
}
}
which converts JSON to Scala object.
When JSON is valid (i.e. {"name":"name","number": 0}
it works fine. However, when number
is in quotes {"name":"name","number":"0"}
I get an error: validate.error.expected.jsnumber
.
Is there a way to implicitly convert String
to Int
in such a case (assuming that the number is valid) ?