I have a class with many many fields, all of which are Optional when deserialized from JSON.
Is there a way to convert this to a class with matching all non-Optional types.
My optional class
@JsonDeserialize public class VeryOptionalClass { @Value.Parameter @JsonProperty("fieldOne") public Optional<String> fieldOne() @Value.Parameter @JsonProperty("fieldTwo") public Optional<Double> fieldTwo() @Value.Parameter @JsonProperty("fieldOne") public Optional<String> fieldThree() @Value.Parameter @JsonProperty("fieldThree") public Optional<SomeObject> fieldFour() ---skip a few lines here--- @Value.Parameter @JsonProperty("fieldTwentyfive") public Optional<String> fieldHundredTwentyFive() } My desired result:
public class TotallyNonOptionalClass { public String fieldOne() public Double fieldTwo() public String fieldThree() public SomeObject fieldFour() ---skip a few lines here--- public Integer fieldHundredTwentyFive() } If all the Optional fields are populated, is this possible to do elegantly? I know I can do a builder like:
VeryOptionalClass myOptionalClass = someProvider(someInput) TotallyNonOptionalClass.builder() .fieldOne(myOptionalClass.fieldOne().get()) .fieldTwo(myOptionalClass.fieldTwo().get()) .fieldThree(myOptionalClass.fieldThree().get()) .fieldFour(myOptionalClass.fieldFour().get()) ---skip a few lines here--- .fieldHundredTwentyFive(myOptionalClass.fieldHundredTwentyFive().get()) .build() Or if I wanted I could use Optional.orElse(someDefault) and list out all the fields in the builder. But let's say I know that all the optionals are non-empty, is there a way to achieve the same thing without a very long Builder?
PS. My class doesn't actually have 125 fields, but it is long and I'd rather not have to make changes in both VeryOptionalClass and TotallyNonOptionalClass every time I add a field/function.
ObjectMapper.convertValue().