score:1

Accepted answer

yes, this is possible by using the readwithdefault method.

having:

case class applyrequest(a: string, b: string, c: option[string], d: option[int])

private val json =
"""
  |{
  |  "a": "test1",
  |  "b": null,
  |  "c": null,
  |  "d": 1
  |}
""".stripmargin

the reads is defined:

implicit val readobj: reads[applyrequest] = (
    (jspath \ "a").read[string] and
    (jspath \ "b").readwithdefault[string]("null") and // default value here
    (jspath \ "c").readnullable[string] and
    (jspath \ "d").readnullable[int]
  ) (applyrequest.apply _)

parsed:

val parsedjsvalue = json.parse(json)
val parsedapplyrequest = json.fromjson[applyrequest](parsedjsvalue).get
println(parsedapplyrequest)            // applyrequest(test1,null,none,some(1))
println(parsedapplyrequest.b.getclass) // class java.lang.string

Related Query

More Query from same tag