I have a trait used to inject json decoder as dependency to components of my project:
trait JsonDecoder {
def apply[T](s: String): Option[T]
}
When I try to implement it with Circe:
import io.circe.generic.auto._
import io.circe.parser.decode
case class CirceJsonDecoder() extends JsonDecoder {
def apply[T](s: String): Option[T] = {
decode[T](s).fold(_ => None, s => Some(s))
}
}
and run:
case class C()
def test(d: JsonDecoder) = d[C]("{}")
test(CirceJsonDecoder())
it does not compile with error:
could not find implicit value for parameter decoder: io.circe.Decoder[T]
I tried to add ClassTag
, TypeTag
or WeakTypeTag
context bounds for T
but it still can not find implicit value for Decoder
.
I can not add Decoder
context bound or implicit parameter to JsonDecoder.apply
because components what use it should not know about implementation details.
How should I provide implicit io.circe.Decoder
? May be there is some way to get it from TypeTag
?