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

Useless compile error while lacking encoder/decoder #35

Closed
jilen opened this issue Aug 15, 2015 · 3 comments
Closed

Useless compile error while lacking encoder/decoder #35

jilen opened this issue Aug 15, 2015 · 3 comments

Comments

@jilen
Copy link

jilen commented Aug 15, 2015

If there is no encode for date, then compiler complains

 [error] /home/jilen/workspace/jfc-test/src/main/scala/Foo.scala:26: diverging implicit expansion for type io.circe.Encoder.Secondary[this.Out]
[error] starting with method encodeCaseClass in trait GenericInstances

Is there any chance to improve the error reporting ?

import io.circe._
import io.circe.generic.auto._
import io.circe.syntax._

import java.util.Date
case class Foo(
  date: Date,
  number: Int,
  str: String)

object App {

  def encodesDate(fmt: String): Encoder[Date] = new Encoder[Date] {
    def apply(a: Date) = {
      val sdf = new java.text.SimpleDateFormat(fmt)
      Json.string(sdf.format(a))
    }
  }



  def main(args: Array[String])  {
    //implicit val DateEncodes = encodesDate("yyyy-MM-dd HH:mm:ss")
    val f = Foo(new Date, 1, "fff")
    println(f.asJson.noSpaces)
  }
}
@travisbrown
Copy link
Member

Yeah, that's a pretty terrible error message—thanks for pointing it out.

The implementation of the generic derivation mechanism will be changing with #30, which I'm hoping to get done tomorrow, so after that dust settles I'll revisit this and #34, since things might be a little different by then. We should be able to use @implicitNotFound in a few places to improve these messages a lot (see for example this message in Finch).

@djx314
Copy link
Contributor

djx314 commented Aug 15, 2015

+1 to use implicitNotFound !
To decode and encode date, juest implicit

  implicit val dateTimeEncoder: Encoder[DateTime] = Encoder.instance(a => a.getMillis.asJson)
  implicit val dateTimeDecoder: Decoder[DateTime] = Decoder.instance(a => a.as[Long].map(new DateTime(_)))

Because of date has many format, I suggest that defined it in the documention and don't add a default format in the jfc.

@travisbrown
Copy link
Member

I'm going to go ahead and close this since the following is a huge improvement:

<console>:59: error: could not find implicit value for parameter e: io.circe.Encoder[Foo]
           println(f.asJson.noSpaces)
                     ^

I'm not actually sure there's much additional useful information we could add with @implicitNotFound in this case, apart from maybe a pointer to the docs.

julienrf pushed a commit to scalacenter/circe that referenced this issue May 13, 2021
…ling

Issue with decoding longs in argonaut support
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

3 participants