Scala Play 2 - 将JsValue转换为模型对象

时间:2014-11-30 10:58:55

标签: json scala playframework-2.0 json-deserialization

我有一个Scala案例类

case class SmsGatewayConfig(oracle_id: Long,sms_type: String,seconds_of_high_prio: String,allowed_operators: String,allowed_organizations: String, phone_filter: String, min_throughput:Long,max_throughput: Long,comment: String, enabled: Boolean,isGwDedicated: Boolean) 和伴侣对象

object SmsGatewayConfig {
  implicit val smsGatewayConfig: Reads[SmsGatewayConfig] = (
    (JsPath \ "oracle-id").read[Long] and
      (JsPath \ "sms-type").read[String] and
      (JsPath \ "seconds-of-high-prio").read[String] and
      (JsPath \ "allowed-operators").read[String] and
      (JsPath \ "allowed-organizations").read[String] and
      (JsPath \ "phone-filter").read[String] and
      (JsPath \ "min-throughput").read[Long] and
      (JsPath \ "max-throughput").read[Long] and
      (JsPath \ "comment").read[String] and
      (JsPath \ "enabled").read[Boolean] and
      (JsPath \ "isGwDedicated").read[Boolean]
    )(SmsGatewayConfig.apply _)
}

和JsValue喜欢像{"oracle-id":6877851,"sms-type":["1-100"],"allowed-operators":["all"," unknown"],"phone-filter":["+7xxxxxxxxxx"],"max-throughput":1000000,"comment":"","enabled":false}

这样的字符串

但是如果我尝试val obj=configs.as[SmsGatewayConfig]我会得到太多异常[JsResultException: JsResultException(errors:List((/allowed-operators,List(ValidationError(error.expected.jsstring,WrappedArray()))), (/isGwDedicated,List(ValidationError(error.path.missing,WrappedArray()))), (/min-throughput,List(ValidationError(error.path.missing,WrappedArray()))), (/phone-filter,List(ValidationError(error.expected.jsstring,WrappedArray()))), (/sms-type,List(ValidationError(error.expected.jsstring,WrappedArray()))), (/allowed-organizations,List(ValidationError(error.path.missing,WrappedArray()))), (/seconds-of-high-prio,List(ValidationError(error.path.missing,WrappedArray())))))]而且我不知道我为解决这个问题做了什么。抱歉我的英文不好

1 个答案:

答案 0 :(得分:2)

在您的JSON中,sms-typeallowed-operatorsphone-filter是数组。 因此,您需要将Reads对象中的以下内容更改为:

object SmsGatewayConfig {
  implicit val smsGatewayConfig: Reads[SmsGatewayConfig] = (
    (JsPath \ "oracle-id").read[Long] and
      (JsPath \ "sms-type").read[Seq[String]] and
      (JsPath \ "seconds-of-high-prio").read[String] and
      (JsPath \ "allowed-operators").read[Seq[String]] and
      (JsPath \ "allowed-organizations").read[String] and
      (JsPath \ "phone-filter").read[Seq[String]] and
      (JsPath \ "min-throughput").read[Long] and
      (JsPath \ "max-throughput").read[Long] and
      (JsPath \ "comment").read[String] and
      (JsPath \ "enabled").read[Boolean] and
      (JsPath \ "isGwDedicated").read[Boolean]
    )(SmsGatewayConfig.apply _)
}

您还需要更新案例类以表示这些字段是数组而不仅仅是字符串:

case class SmsGatewayConfig(oracle_id: Long, sms_type: Seq[String], seconds_of_high_prio: String, allowed_operators: Seq[String], allowed_organizations: String, phone_filter: Seq[String], min_throughput: Long, max_throughput: Long, comment: String, enabled: Boolean, isGwDedicated: Boolean)

此外,您的JSON输入中似乎缺少某些字段:seconds-of-high-prioallowed-organizationsmin-throughputisGwDedicated

要在代码中表示这一点,请在案例类中使用Option,在Reads对象中使用readNullable

case class SmsGatewayConfig(oracle_id: Long, sms_type: Seq[String], seconds_of_high_prio: Option[String], allowed_operators: Seq[String], allowed_organizations: Option[String], phone_filter: Seq[String], min_throughput: Option[Long], max_throughput: Long, comment: String, enabled: Boolean, isGwDedicated: Option[Boolean])

object SmsGatewayConfig {
  implicit val smsGatewayConfig: Reads[SmsGatewayConfig] = (
    (JsPath \ "oracle-id").read[Long] and
      (JsPath \ "sms-type").read[Seq[String]] and
      (JsPath \ "seconds-of-high-prio").readNullable[String] and
      (JsPath \ "allowed-operators").read[Seq[String]] and
      (JsPath \ "allowed-organizations").readNullable[String] and
      (JsPath \ "phone-filter").read[Seq[String]] and
      (JsPath \ "min-throughput").readNullable[Long] and
      (JsPath \ "max-throughput").read[Long] and
      (JsPath \ "comment").read[String] and
      (JsPath \ "enabled").read[Boolean] and
      (JsPath \ "isGwDedicated").readNullable[Boolean]
    )(SmsGatewayConfig.apply _)
}

您所拥有的所有错误,或者至少大部分错误都应该随着这些变化而消失。

作为一个小细节,我建议使用camelCase表示法,而不是用于命名案例类字段的underscore_based命名。