Suppose I have the following case classes that need to be serialized as JSON objects using circe:
@JsonCodec
case class A(a1: String, a2: Option[String])
@JsonCodec
case class B(b1: Option[A], b2: Option[A], b3: Int)
Now I need to encode val b = B(None, Some(A("a", Some("aa")), 5)as JSON, but I want to be able to control whether it is output as
{
"b1": null,
"b2": {
"a1": "a",
"a2": "aa"
},
"b3": 5
}
or
{
"b2": {
"a1": "a",
"a2": "aa"
},
"b3": 5
}
Using Printer dropNullKeysconfig, for example. b.asJson.noSpaces.copy(dropNullKeys = true)will cause output to exit None, while setting it to falsewill encode Noneas null( see also this question ). But how can you control this parameter based on the field?
source
share