Open calii23 opened 1 year ago
Idk how I ran into this issue, but this issue about defaultValue
and required
and may help coming up with a solution.
Yes, the annotation mechanism would be
@JsonProperty(default = "10")
but the real challenges are:
JsonDeserializer
interface)For (1) we might be able to use KeyDeserializer
, possibly extending support for types provided. For (2) we would probably need to limit this to @JsonCreator
provided values, same as @JsonProperty.required
Also note that this is not really Record-specific; if this was to be supported, could and should work for POJOs too, I think.
It would be very useful in some situations to define default values when deserializing into a record. It is only possible in the moment to define default values using the
@JsonSetter
annotation and then define custom logic in the setter. But as this is not possible when using java records, as in this case all properties must be passed using the constructor and then the object becomes immutable.It might be possible to use this:
But the readability (especially when we have more than 2 properties) is not very good, and there is a lot of boilerplate code here. This variant might be much more readable:
or:
This may also allow to handle default value in case the property is missing, so that it can still be set explicitly to
null
.I know this ticket is kinda duplicated from #39 but I thought I may open a new one specifically related to java records and not only to default from
null
more to default when the property is missing.