Last active
June 23, 2017 08:33
-
-
Save Leammas/8df7cd72de57c66a1cb639c6b2ff5824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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