circe / circe-generic-extras

Configurable generic derivation for Circe
Apache License 2.0
62 stars 28 forks source link

Codec derivation - `dropNullValues` #323

Open kamilkloch opened 1 year ago

kamilkloch commented 1 year ago

For now it is impossible to convenietly generate encoder that does not produce null values at the output.

Scenario:

@JsonCodec case class Params(p1: String, p2: Option[Int])
def process(a: Json): Unit = ???

val params = Params("abc", None)
process(json"""{"requestId": 123, "params": $params}""")

And I want the a not to contain null values. I know I can:

  1. preprocess a in the process() method - apply dropNullValues, use customized printer, etc
  2. postprocess json where I use interpolation
  3. customize codec by .mapJson(.dropNullValues)

First and second one seems like wrong place to solve this issue - its like workaround for misbehaving codec.

Third option requires lots of bolierplate code: Switching from annotation to manual derivation (creation of companion objects, etc), and then mapping derived encoder output.

I believe most correct solution would be to add io.circe.generic.extras.Configuration.dropNullValues. Then one could use it with derived codecs as well as with @ConfiguredJsonCodec.