Generic derivation of AnyVal types with Circe
<p>I want to derive Encoder instances for value classes. With the <code>semiauto</code> mechanism I am not able to derive nested classes.</p>

<p>Image the following case class structure</p>

<pre><code>{
  case class Recipient(email: Recipient.Email, name: Recipient.Name) 

  object Recipient {
     case class Email(value: String) extends AnyVal
     case class Name(value: String) extends AnyVal
  }
}
</code></pre>

<p>In an ammonite shell ( also add the Recipient case classes )</p>

<pre><code>load.ivy("io.circe" %% "circe-core" % "0.6.1")
load.ivy("io.circe" %% "circe-generic" % "0.6.1")

import io.circe._
import io.circe.generic.semiauto._
import io.circe.syntax._ 
</code></pre>

<p>Now deriving a decoder for <code>Email</code> results as expected in</p>

<pre><code>Recipient.Email("ab@cd.com").asJson(deriveEncoder[Recipient.Email]) 
Json = {
  "value" : "ab@cd.com"
}
</code></pre>

<p>Deriving an <code>Encoder[Recipient]</code> doesn't work</p>

<pre><code>deriveDecoder[Recipient]

could not find Lazy implicit value of type 
io.circe.generic.decoding.DerivedDecoder[$sess.cmd5.Recipient]
</code></pre>

<p>What I would like to do is deriving an <code>Encoder[Recipient.Email]</code> that returns the wrapped type. This little piece works if I derive the codec explicitly.</p>

<pre><code>import shapeless.Unwrapped

implicit def encodeAnyVal[W &lt;: AnyVal, U](
        implicit unwrapped: Unwrapped.Aux[W, U],
                 encoderUnwrapped: Encoder[U]): Encoder[W] = {
        Encoder.instance[W](v =&gt; encoderUnwrapped(unwrapped.unwrap(v)))
}

Recipient.Email("ab@cd.com").asJson(encodeAnyVal[Recipient.Email, String])
res11: Json = "ab@cd.com"
</code></pre>

<p>Still I can't derive a <code>Encoder[Recipient]</code></p>

<pre><code>implicit val emailEncoder: Encoder[Recipient.Email] = encodeAnyVal[Recipient.Email, String] 
implicit val nameEncoder: Encoder[Recipient.Name] = encodeAnyVal[Recipient.Name, String]

deriveDecoder[Recipient] 
cmd14.sc:1: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[$sess.cmd5.Recipient]
</code></pre>

<p>Has anyone done something similar?</p>

<p>Thanks in advance,
Muki</p>