You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
public record PageRequest(
intpage,
@JsonProperty(defaultValue = "20") // this parameter already exists, just doesn't do anything in the momentintpageSize
) {
}
or:
public record PageRequest(
intpage,
@JsonDefaultInt(20)
intpageSize
) {
}
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.
The text was updated successfully, but these errors were encountered:
cowtowncoder
changed the title
Default values in Java 14 records
Support default values in Java 14 records
Jun 12, 2023
How to deserialize values from JSON Strings (cannot use JsonDeserializer interface)
How to apply defaults
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.
cowtowncoder
changed the title
Support default values in Java 14 records
Support default values in Java 14 records (and POJOs too?)
Jan 2, 2024
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.The text was updated successfully, but these errors were encountered: