Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle optional fields in JSON parsing in play 2.1

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.

like image 265
NickStoughton Avatar asked Jan 08 '13 03:01

NickStoughton


2 Answers

OK ... so the answer is very simple. Use

fomatOpt()

for optional fields. So the test formatter now looks 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").formatOpt[String] 
  )(Test.apply, unlift(Test.unapply))

  def fromJson(js: String): Test = {
    Json.fromJson[Test](Json.parse(js)).fold(
      valid   = { t => t},
      invalid = { e => {
        println("BAD JSON!")
        null
      }} 
    )
  }
}
like image 162
NickStoughton Avatar answered Sep 21 '22 09:09

NickStoughton


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)
    ...
}
like image 21
sndyuk Avatar answered Sep 18 '22 09:09

sndyuk