Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no implicit values were found that match type io.circe.export.Exported[io.circe.Decoder[T]] #2008

Open
ouardateaicha opened this issue Jul 29, 2022 · 1 comment

Comments

@ouardateaicha
Copy link

ouardateaicha commented Jul 29, 2022

Hello,

I have this code and i don t understand why it not working:

import io.circe.generic.semiauto.deriveEncoder
import io.circe.generic.semiauto.deriveDecoder
import io.circe.parser.decode
import io.circe.syntax._
import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
//generic converter
object CirceConverter {


  def toJson[T:ClassTag](t: T)(implicit encoder: Encoder[T]): Try[String] = Try(t.asJson.noSpaces)

  def convertToObj[T](jsonStr: String)(implicit decoder: Decoder[T]): Try[T] =
    decode[T](jsonStr) match {
      case Right(value) => Success(value)
      case Left(error)  => Failure(error)
    }

}


case class Test[T](id: Long,val result: Option[T])


object Test {
  implicit def RcpEncoder[T: Encoder]: Encoder[Test[T]] = deriveEncoder
  implicit def RcpDecoder[T: Decoder]: Decoder[Test[T]] = deriveDecoder

}


class service(){


def get[T](url:String):Try[RPCResult[T]] =Try(

val strJson= getJsonResult(url)

CirceConverter.convertToObj[Test[T]](strJson)

)

..

}

When i run it, i have this error :

no implicit argument of type io.circe.Decoder[com.test.Test[T]] was found for parameter decoder of method convertToObj in object CirceConverter.
I found:

    com.test.Test.RcpDecoder[T](
      io.circe.Decoder.importedDecoder[T](
        /* missing */summon[io.circe.export.Exported[io.circe.Decoder[T]]]
      )
    )

But no implicit values were found that match type io.circe.export.Exported[io.circe.Decoder[T]].
                CirceConverter.convertToObj[Test[T]](strJson)

build.sbt:

val circeVersion = "0.14.1"

libraryDependencies ++= Seq(
  "io.circe" %% "circe-core",
  "io.circe" %% "circe-generic",
  "io.circe" %% "circe-parser"
).map(_ % circeVersion)

Any idea how to solve this?

many thanks

@amumurst
Copy link

(I don't know how familiar you are with these concepts, so I just made an explanation which might be too low/high level)

In this code

def get[T](url:String):Try[RPCResult[T]] =Try(
   val strJson= getJsonResult(url)
   CirceConverter.convertToObj[Test[T]](strJson)
)

You use the method converteToObj with type Test[T]. If we look at that method it requires a decoder for Test[T] to be available in scope (implicit).

Earlier in the code you have
implicit def RcpDecoder[T: Decoder]: Decoder[Test[T]] = deriveDecoder which says that if I have a decoder for T, I can produce a decoder for Test[T].

But if we inspect the get method you have we can see that there exists no evidence that the T has a decoder available for it.

To fix it you would have to provide that evidence, and that can be done by changing the method signature to one of these depending on you preferred style (you have used both above, for this code they are the same).

def get[T: Decoder](url:String):Try[RPCResult[T]]
//or
def get[T](url:String)(implicit decoder: Decoder[T]):Try[RPCResult[T]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants