找不到隐式参数

时间:2015-09-25 16:48:43

标签: json scala playframework implicit

我在弄清楚为什么编译器抱怨没有找到读取的隐式参数时遇到了一些麻烦,因为我几乎可以肯定它在范围内。错误如下:

错误:(13,18)找不到类型为Config的Json反序列化程序。尝试为此类型实现隐式读取或格式。     test.validate [Config] .map {                  ^

错误:(13,18)方法验证的参数不足:(隐式rds:play.api.libs.json.Reads [Config])play.api.libs.json.JsResult [wings.m2m.conf。 model.Config。 未指定的值参数rds。     test.validate [Config] .map {                  ^

它出现在以下代码中:

import play.api.libs.json._
import play.api.libs.json.Reads._
import Config.JsonImplicits._

import scala.util.Try

object Test {
  def main(args: Array[String]) {

    val test = Json.obj("action" -> Config.Action.nameAcquisitionRequest.toString, "value" -> "hola")
    test.validate[Config].map {
      t => println(t)
        t
    }
  }
}

/**
 * Config companion object
 */
object Config {

  type ValueType = String

  val ActionKey = "action"

  val ValueKey = "value"

  object Action extends Enumeration {

    type Action = Value

    val nameAcquisitionRequest = Value("nameAcquisitionRequest")
    val nameAcquisitionReject = Value("nameAcquisitionReject")
    val nameAcquisitionAck = Value("nameAcquisitionAck")

    val broadcast = Value("broadcast")

  }

  /**
   * Json implicit conversions
   */
  object JsonImplicits {

    implicit object ConfigReads extends Reads[Config] {

      def hTypeCast(action: Config.Action.Value, value: Config.ValueType): Config = {
        action match {
          case Config.Action.nameAcquisitionRequest => NameAcquisitionRequest(value)
          case Config.Action.nameAcquisitionReject => NameAcquisitionReject(value)
          case Config.Action.nameAcquisitionAck => NameAcquisitionAck(value)
        }
      }

      override def reads(json: JsValue): JsResult[Config] = json match {
          case json: JsObject =>
            val action = (json \ ActionKey).as[String]
            Try(Config.Action.withName(action)) map {
              a =>
                val value = (json \ ValueKey).as[String]
                JsSuccess(hTypeCast(a, value))
            } getOrElse (JsError("Can't convert to Config"))
          case _ => JsError("Can't convert to Config")
        }
      }


    implicit object ConfigWrites extends OWrites[Config] {

      def jsObjectCreator(action: Config.Action.Value, value: Config.ValueType): JsObject = {
        Json.obj(ActionKey -> action.toString, ValueKey -> Json.toJson(value))
      }

      override def writes(o: Config): JsObject = o match {
        case c: NameAcquisitionRequest => jsObjectCreator(Config.Action.nameAcquisitionRequest, c.value)
        case c: NameAcquisitionReject => jsObjectCreator(Config.Action.nameAcquisitionReject, c.value)
        case c: NameAcquisitionAck => jsObjectCreator(Config.Action.nameAcquisitionAck, c.value)
      }
    }
  }

}

sealed trait Config {
  val value: Config.ValueType
}

/**
 * Intermediate config message
 * @param value
 */
case class NameAcquisitionRequest(override val value: String)
  extends Config

case class NameAcquisitionReject(override val value: String)
  extends Config

case class NameAcquisitionAck(override val value: String)
  extends Config

case class Broadcast(override val value: String)
  extends Config

在Test对象上执行main方法时发生错误。要使此示例有效,请确保在SBT中添加以下依赖项:“com.typesafe.play”%%“play-json”%“2.4.1”。而且我不确定,但也许需要这个解析器:解析器+ =“Typesafe Repo”在“http://repo.typesafe.com/typesafe/releases/

1 个答案:

答案 0 :(得分:0)

我不确定你想要达到的目的是什么,这是否解决了你的问题但是你走了:

test.validate[Config](Config.JsonImplicits.ConfigReads).map {
  t => println(t)
    t
}