How to handle optional fields in JSON parsing in p

2019-04-20 04:31发布

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.

2条回答
冷血范
2楼-- · 2019-04-20 04:58

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
      }} 
    )
  }
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-20 05:06

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)
    ...
}
查看更多
登录 后发表回答