Assume a JSON structure with multiple optional fields. With classes, you can do something like
public static final class Foo {
@JsonProperty("x")
private int x = 1;
@JsonProperty("y")
private int y = 2;
@JsonProperty("z")
private int z = 3;
}
which defines default values for the fields in case it is not present in the provided json. Can this be done with records as well?
public record Foo(int x, int y, int z) {
}
Constructor overloading is obviously not an option, and as far as I know you could only have a single @JsonCreator
annotation anyway.
A custom deserializer should do the trick, but is there any other way, like an annotation that provides a default value to use in the constructor of the record in case it is not provided in the json?