简体   繁体   中英

scala implicit passing to parameter

To convert JsValue to custom Class, used this code.

def Foo(today : String):String = {
    implicit def read (js: JsValue) : Reads[ResponseBasicModel[String]] = Reads[ResponseBasicModel[String]](
      js => JsSuccess(ResponseBasicModel[String](
        ReturnValue = js.\("ReturnValue").toString()
        )
      )
    )

    CallAPI[ResponseBasicModel[String]](
        "GET",
        "URL"
        ,15.second).ReturnValue
  }

CallAPI :

def CallAPI[A](httpMethod: String, subURL: String, timeout: FiniteDuration)(implicit m: scala.reflect.Manifest[A], read: Reads[A]) :A = {
/...
     Json.parse(robots.toString()).as[A]
}

But It return error

Error:(20, 47) No Json deserializer found for type finance.remittance.data.ResponseBasicModel[String]. Try to implement an implicit Reads or Format for this type. return CallAPI[ResponseBasicModel[String]](

Is there any solution?

I Solved this problem with this code.

Writes is null because I do not use writes function.

def Foo(today : String):String = {
    implicit object ResponseBasicModelFormat extends Format[ResponseBasicModel[String]]{
    def reads(js: JsValue) =JsSuccess(ResponseBasicModel[String](
        ReturnValue = js.\("ReturnValue").toString()
      ))
    def writes(res : ResponseBasicModel[String]): JsValue=null
  }

    CallAPI[ResponseBasicModel[String]](
        "GET",
        "URL"
        ,15.second).ReturnValue
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM