I am generating a set of methods that will instantiate a class from a Play JsObject, e.g.
class Clazz(val1: Int = 1, val2: String, val3: Option[Int])
def createFromJson(json: JsObject) {
Clazz((json \ "val1").as[Int], (json \ "val2").as[String], (json \ "val3").as[Option[Int]])
}
Several of our classes have more than 22 fields, so it's not feasible to use a default Writes/Formats to do this (and anyway, the default value problem would remain).
I would like to be able to say e.g. (json \ "val1").asOpt[Int].getOrElse(1)
for parameters that have default values. I could create a val defaultValues: Map[String, JsValue]
variable with all of the class's default values, or I could create a val defaultValues: JsObject
that I merge with the input json, but ideally I'd like to be able to pull the default value directly from the class, otherwise we'll inevitably update the constructor's default values but not the defaultValues
variable's default values or vice versa.
Is there a way to do this?