Reputation: 1071
I have the following Reads object:
implicit val branchRead: Reads[BranchApplyRequest] = (
(JsPath \ "act").read[String] and
(JsPath \ "st").read[String] and
(JsPath \ "nts").read[String] and
(JsPath \ "bk").read[Int]
) (BranchApplyRequest.apply _)
The problem is that when one of these fields doesn't come from the browser, I get an exception and the program fails. Ideally, the non present field will be available but undefined. How to achieve that?
Upvotes: 0
Views: 109
Reputation: 48133
Use readNullable
instead. For example, if act
is Optional
:
implicit val branchRead : Reads[BranchApplyRequest] = (
(JsPath \ "act").readNullable[String] and
(JsPath \ "st").read[String] and
(JsPath \ "nts").read[String] and
(JsPath \ "bk").read[Int]
)(BranchApplyRequest.apply _)
And of course your BranchApplyRequest
would be like this:
case class BranchApplyRequest(act: Option[String], ...)
Upvotes: 2