plokhotnyuk / jsoniter-scala

Scala macros for compile-time generation of safe and ultra-fast JSON codecs + circe booster
MIT License
744 stars 99 forks source link

totally Skip Serialization of optional class field. #1209

Open samuelorji opened 3 weeks ago

samuelorji commented 3 weeks ago

Hi,

Is there a way to completely skip the serialization of an optional class field with a default value ?

As an example,

say I have a class Foo:


case class Foo(
         name : Option[String] = None
         age : Int
   )

and I have this JSON:

 {
     "name" : "Jack",
      "age" : 23
 }

Is there a way I can make the JsonCodecMaker skip the serialization of the "name" field in the json and by default just use the default value supplied in the case class. I tried using this config, but it doesn't work as expected because according to the doc, it will skip serialization if the field is the same as the default value, whereas I'm looking for an option where it completely skips the serialization of the field irrespective of the json value

plokhotnyuk commented 3 weeks ago

Have you tried if @transient from the com.github.polokhotnyuk.jsoniter_scala.macros package (or from the scala package with CodecMakerConfig.withScalaTransientSupport(true) argument for the make call) placed at the field definition works for your case?

Please check both approaches shown in these tests.