I'm facing some difficulties while trying to convert JSON response which contains properties with under_score, to DTO with lowerCamelCase properties.
JSON for example:
{
"promoted_by": "",
"parent": "",
"caused_by": "jenkins",
"watch_list": "prod",
"u_automation": "deep",
"upon_reject": "cancel"
}
DTO for example:
@Data
public class TicketDTO {
private String promotedBy;
private String parent;
private String causedBy;
private String watchList;
private String uAutomation;
private String uponReject;
}
Mapper:
@Mapper(componentModel = "spring")
public interface ITicketMapper {
default TicketDTO toDTO(JsonObject ticket) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
return gson.fromJson(incident, TicketDTO.class);
}
}
This example is not working of course, I would like to know if it's possible to do this conversion with Gson.
Appriciate your help.
You should use LOWER_CASE_WITH_UNDERSCORES
Using this naming policy with Gson will modify the Java Field name from its camel cased form to a lower case field name where each word is separated by an underscore (_). Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
The UPPER_CAMEL_CASE is used for different purpose
Using this naming policy with Gson will ensure that the first "letter" of the Java field name is capitalized when serialized to its JSON form.