title: Circe - Use default fields in case class when decoding/encoding json
body: <p>Lets say I have this case class:</p>
  
  <p><code>case class Foo(bar: String, baz: Boolean = false)</code></p>
  
  <p>which is used in when decoding/encoding API requests/responses using <a href="https://github.com/hseeberger/akka-http-json" rel="noreferrer">akka-http-json</a></p>
  
  <p>in an example similar to <a href="https://github.com/hseeberger/akka-http-json/blob/master/akka-http-circe/src/test/scala/de/heikoseeberger/akkahttpcirce/ExampleApp.scala" rel="noreferrer">this</a>:</p>
  
  <pre><code>import akka.actor.ActorSystem
  import akka.http.scaladsl.Http
  import akka.http.scaladsl.server.Directives
  import akka.stream.{ ActorMaterializer, Materializer }
  import scala.io.StdIn
  
  object ExampleApp {
  
    private final case class Foo(bar: String, baz: Boolean = false)
  
    def main(args: Array[String]): Unit = {
      implicit val system = ActorSystem()
      implicit val mat    = ActorMaterializer()
  
      Http().bindAndHandle(route, "127.0.0.1", 8000)
  
      StdIn.readLine("Hit ENTER to exit")
      system.terminate()
    }
  
    private def route(implicit mat: Materializer) = {
      import Directives._
      import FailFastCirceSupport._
      import io.circe.generic.auto._
  
      pathSingleSlash {
        post {
          entity(as[Foo]) { foo =&gt;
            complete {
              foo
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>This works fine as long as the json message includes the <code>baz</code> field. However, I want to be able to send a json message <code>{bar: "something"}</code> and let the result use <code>Foo</code>'s default value for <code>baz</code>. Is there any configuration in <code>circe</code> or <code>akka-http-json</code> that could make this work?</p>
  
  <p>Also, would be nice to ignore the <code>baz</code> field when encoding to json again, but this is not that important.</p>
  
  <p><strong>Edit</strong>: </p>
  
  <p>I know I can do something like this:</p>
  
  <pre><code>implicit val fooEncoder: Encoder[Foo] = new Encoder[Foo] {
      final def apply(a: Foo): Json = Json.obj(
        ("id", Json.fromString(a.bar))
      )
    }
  
  implicit val fooDecoder: Decoder[Foo] = new Decoder[Decoder] {
    final def apply(c: HCursor): Decoder.Result[Decoder] =
      for {
        bar &lt;- c.downField("bar").as[String]
      } yield {
        Foo(bar)
      }
  }
  </code></pre>
  
  <p>but was hoping for an easier-to-maintain solution, solving the general case of not requiring the default fields in the json message.</p>