title: Encoding Scala None to JSON value using circe
body: <p>Suppose I have the following case classes that need to be serialized as JSON objects using circe:</p>
  
  <pre><code>@JsonCodec
  case class A(a1: String, a2: Option[String])
  
  @JsonCodec
  case class B(b1: Option[A], b2: Option[A], b3: Int)
  </code></pre>
  
  <p>Now I need to encode <code>val b = B(None, Some(A("a", Some("aa")), 5)</code> as JSON but I want to be able to control whether it is output as</p>
  
  <pre><code>{
    "b1": null,
    "b2": {
            "a1": "a",
            "a2": "aa"
          },
    "b3": 5
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>{
    "b2": {
            "a1": "a",
            "a2": "aa"
          },
    "b3": 5
  }
  </code></pre>
  
  <p>Using <code>Printer</code>'s <a href="https://github.com/circe/circe/blob/master/modules/core/shared/src/main/scala/io/circe/Printer.scala#L37" rel="noreferrer"><code>dropNullKeys</code></a> config, e.g. <code>b.asJson.noSpaces.copy(dropNullKeys = true)</code> would result in omitting <code>None</code>s from output whereas setting it to <code>false</code> would encode <code>None</code>s as <code>null</code> (<a href="https://stackoverflow.com/questions/41721970/avoid-serialize-to-null-using-circe-json-serializer">see also this question</a>). But how can one control this setting on a per field basis?</p>