Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

totally Skip Serialization of optional class field. #1209

Open
samuelorji opened this issue Oct 8, 2024 · 1 comment
Open

totally Skip Serialization of optional class field. #1209

samuelorji opened this issue Oct 8, 2024 · 1 comment
Labels

Comments

@samuelorji
Copy link

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
Copy link
Owner

plokhotnyuk commented Oct 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants