为什么隐式读取找不到我的对象类型scala

时间:2019-02-27 15:15:31

标签: json scala playframework implicit

在写隐式写入以将自定义类型读回到scala值后尝试写隐式读取

`

implicit val locationReads: Reads[Equipment] = (
      (JsPath \ "numberOwned").read[Int] and
        (JsPath \ "name").read[String]
      )(Equipment.apply _)

`

我的自定义类型“设备”具有变量numberOwned和name”,在编写此代码时出现的错误是“无法解析符号设备”。

任何建议表示赞赏!

1 个答案:

答案 0 :(得分:0)

尝试这个,让我知道是否有帮助。

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

  case class Equipment(numberOwned: Int, name: String)

  implicit def equipmentReads: Reads[Equipment] = {
    try {
      JsSuccess(Equipment(
        (v \ "numberOwned").as[Int],
        (v \ "name").as[String]
      ))
    } catch {
      case e: Exception => Reads {
        case _ =>
          JsError(JsonValidationError("Cannot De-serialize Equipment value."))
      }
    }
  }