question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Map[Boolean, T] fails to encode with 'could not find Lazy implicit value of type DerivedAsObjectEncoder[Foo]'

See original GitHub issue

Build Details

Circe : 0.14.0-M1 Scala : 2.12.10

Problem

If there is a case class, with an element that is a Map where key is Boolean, the encoding fails with an unclear error message

could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder[Foo]
not enough arguments for method deriveEncoder: (implicit encode: shapeless.Lazy[io.circe.generic.encoding.DerivedAsObjectEncoder[Foo]])io.circe.Encoder.AsObject[Foo].
Unspecified value parameter encode.

Code

import io.circe.Encoder
import io.circe.syntax._
import io.circe.generic.semiauto.deriveEncoder

case class Foo(boolKeyMap: Map[Boolean, String])

object Foo {
  implicit val encFoo: Encoder[Foo] = deriveEncoder[Foo]
}
println(
  Foo(boolKeyMap = Map(true -> "str")).asJson.spaces2
)

Scastie link

https://scastie.scala-lang.org/0PSRfw5JRASQGBEcHyWAhA

Question

I’m wondering if this is intended behavior and if so, is there scope of improving the error message.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zarthrosscommented, Jan 14, 2022

@praateekmahajan I think the problem with us implementing a Boolean KeyEncoder or KeyDecoder is going to be choosing a canonical representation. For all the key encoder/decoders so far there is a very clear representation to choose, including URI and UUID. But for boolean, do we go with TRUE, true, True, or 1? We cannot choose all of them, because you could have multiple of those keys within a Json Objects. I think the safest thing for us, as a library would be to let any user who needs a KeyEncoder[Boolean] to implement the specific one they want.

For instance, you could add this next to your encoder and solve your problem, with a bonus decoder too: (scastie link)

  implicit val encBoolKey: KeyEncoder[Boolean] = KeyEncoder[String].contramap(_.toString)
  implicit val decBoolKey: KeyDecoder[Boolean] = KeyDecoder.instance {
    case "true" => Option(true)
    case "false" => Option(false)
    case _ => None
  }
0reactions
praateekmahajancommented, Jan 14, 2022

Thank you for engaging in the conversation, I agree it’s not common and could be better defined in a case class with two values.

For my understanding, would there be a reason to not add the Boolean encoder to KeyEncoder? Not common is a good reason that it isn’t already there but wondering if it’s a good enough reason to not include it further (assuming the time to create and merge the PR is free)? I see URI in the KeyEncoder which makes me think that’s a further rarer key, yet possible key, similarly Boolean, rare key yet possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

could not find Lazy implicit value of type io.circe.generic ...
could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder (Map custom key) #1626.
Read more >
Circe deriveDecoder/deriveEncoder could not find Lazy ...
Circe deriveDecoder/deriveEncoder could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder · Ask Question.
Read more >
circe/circe - Gitter
Error:(32, 68 ) could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[Operation] implicit val OperationDecoder: ...
Read more >
Circe deriveDecoder/deriveEncoder could not find Lazy ...
Coding example for the question Circe deriveDecoder/deriveEncoder could not find Lazy implicit value of type io.circe.generic.encoding.
Read more >
Implicits guide for custom types — tapir 0.x documentation
A could not find implicit value error can be sometimes puzzling, ... In general, when using a custom type in any context, an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found