I was using org.codehaus.jackson.map.ObjectMapper to deserialize a JSON string with default values as follows:
I have the following POJO:
@Data
@Builder
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class CompanyRequest {
@Builder.Default
private String email = "";
private int company;
@Builder.Default
List<UserIdMapping> users = new ArrayList<>();
}
and when executing the following:
CompanyRequest companyRequest = objectMapper.readValue(message, CompanyRequest.class);
where message is:
{
"email": "john@doe.com",
"company": 42,
}
it worked as expected, meaning companyRequest initialized with users as empty ArrayList.
then I switched to com.fasterxml.jackson.databind.ObjectMapper, but users for the same message was null.
I was able to solve it with the following changes:
@Data
@EqualsAndHashCode
@Builder
public class CompanyRequest {
private String email;
private int company;
private List<UserIdMapping> users;
@JsonCreator
public CompanyRequest() {
this.email = "";
this.users = new ArrayList<>();
}
@Builder
public CompanyRequest(String email, int company, List<UserIdMapping> users) {
this.sfAccountId = StringUtils.isEmpty(email) ? "" : email;
this.company = company;
this.users = CollectionUtils.isEmpty(users) ? new ArrayList<>() : users;
}
}
but after knowing that codehaus is actually working as intended without this manipulation I was wondering is there a cleaner way to do it with fasterxml, since fasterxml is the newer version of ObjectMapper,
meaning is it a feature that enables it?