JSON typeclasses that know the difference between null and absent fields
ToJson
and FromJson
Rather than implementing ToJson
and FromJson
by hand, you can generate them automatically using
ToJson
and FromJson
If you like you can even skip the declaration by mixing in AutoToJson
or
AutoFromJson
, or importing nrktkt.ninny.Auto._
.
You can change the name of a field being read to/from JSON using the @JsonName
annotation.
If your case class has optional parameters then you can use their default values when a field is absent by importing the feature flag FromJsonAuto.useDefaults
in the scope of the derivation for your FromJson
.
We don’t usually expect to have null references in Scala. So the default behavior is for null references to be passed to ToJson
instances where they could be handled for a specific type.
However if you need to handle null references generally we have NullPointerBehavior.
By providing an instance of NullPointerBehavior
in the scope of your ToJson
derivation you can control if null references are passed to ToJson
instances or if a value is provided instead. NullPointerBehavior.Ignore
and IgnorePointerBehavior.WriteNull
are provided the typical uses cases.