Scala – How to handle optional fields in JSON parsing in play 2.1

playframeworkplayframework-2.0scala

I'm using the new Play 2.1-RC1 framework and I have a class that has an Option[] field, something like this:

import play.api.libs.json._
import play.api.libs.json.util._
import play.api.libs.json.Reads._
import play.api.libs.json.Writes._
import play.api.libs.json.Format._
import play.api.libs.functional.syntax._

case class Test(name: String, value: Option[String])

object Test {
  implicit val testFormat = (
    (__ \ "name").format[String] and
    (__ \ "value").format[Option[String]] 
  )(Test.apply, unlift(Test.unapply))
  def fromJson(js: String): Test = {
    Json.fromJson[Test](Json.parse(js)).fold(
        valid   = { t => t},
        invalid = { e => {
          val missingField = (e(0)._1).toString.substring(1)
          val badJs = js.trim
          val newJs = badJs.substring(0, badJs.length()-1)+",\""+missingField+"\":null}"
          fromJson(newJs)
        }} 
    )
  }
}

I want to be able to handle JSON strings that omit the optional "value" data, e.g.

val y = """{"name":"someone"}"""

(edited question)
I can rewrite the json string (rather clumsily) as shown in the validation step, but
is there a simpler pattern I can use to supply None for missing Optional fields? Note that this rewrite does not work with nested structures, or anywhere where I can't simply append the missing field.

Best Answer

You can simply do this:

import play.api.libs.json._
import play.api.libs.functional.syntax._

case class Test(name: String, value: Option[String])

implicit val testFormat = Json.format[Test]

def hoge = Action(Json.parse.json) { request => 
    Json.fromJson[Test](request.body)
    ...
}
Related Topic