Skip to content

Instantly share code, notes, and snippets.

@Leammas
Last active June 23, 2017 08:33
Show Gist options
  • Save Leammas/8df7cd72de57c66a1cb639c6b2ff5824 to your computer and use it in GitHub Desktop.
Save Leammas/8df7cd72de57c66a1cb639c6b2ff5824 to your computer and use it in GitHub Desktop.
import io.circe.Encoder
import shapeless.Unwrapped
import io.circe.derivation
import io.circe.syntax._
object Main extends App {
case class FooId(value: String) extends AnyVal
case class Foo(id: FooId, bar: String)
implicit def anyValEncoder[V, U](implicit ev: V <:< AnyVal,
V: Unwrapped.Aux[V, U],
encoder: Encoder[U]): Encoder[V] =
encoder.contramap(V.unwrap)
// produce `Error:scalac: Error while emitting Main.scala key not found: value inst$macro$8` if line below is commented
// implicit val idEncoder = Encoder.instance[FooId](_.value.asJson)
implicit val fooEncoder = derivation.deriveEncoder[Foo]
println(Foo(FooId("abc"), "cba").asJson.noSpaces)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment