title: json Generic decoder with default values using scala with circe
body: <p>I have encountered a weird situation. <br/>
  I m trying to build a method that takes a type and a JSON
  and build it into a case class instance and if needed auto-complete missing key values.<br/>
  So far I managed to do everything separately but not altogether.<br/>
  The case class with its defaults:</p>
  
  <pre><code>case class Foo(a: String = "empty String", b: Option[Int] = Some(1))
  </code></pre>
  
  <p>and when I do the conversion:</p>
  
  <pre><code>import io.circe.generic.extras.auto._
  import io.circe.generic.extras.Configuration
  import io.circe.parser.decode
  implicit val customConfig: Configuration = Configuration.default.withDefaults
  
  println(decode[Foo]("{}"))
  </code></pre>
  
  <p>this is the output I get:</p>
  
  <pre><code>Right(Foo(empty String,Some(1)))
  </code></pre>
  
  <p>and this is working as I expected</p>
  
  <p>but when I put it into a generic method it required a to be an option due to the error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ExceptionInInitializerError
  Caused by: DecodingFailure(Attempt to decode value on failed cursor, List(DownField(a)))
  </code></pre>
  
  <p>so I`m changing the case class to be </p>
  
  <pre><code>case class Foo(a: Option[String] = Some("empty String"), b: Option[Int] = Some(1))
  </code></pre>
  
  <p>and add the decoder:</p>
  
  <pre><code>object Foo{
      implicit val decoder:Decoder[Foo] = deriveDecoder[Foo]
  }
  </code></pre>
  
  <p>to the method:</p>
  
  <pre><code>import io.circe.Decoder
  import io.circe.parser.decode
  
  def convertToObj[T](jsonStr: String)(implicit decoder: Decoder[T]): T = { 
      decode[T](jsonStr)
      match {
        case Right(value) =&gt; value
        case Left(error) =&gt;  throw error
      }
  }
  println(convertToObj[Foo]("{}"))
  </code></pre>
  
  <p>and the output is:</p>
  
  <pre><code>Foo(None,None)
  </code></pre>
  
  <p>so now I have lost my default values that I put and not able to use the automatic decoder as well.</p>
  
  <p>How can I combine my two wishes into one approach?</p>