I have a JSON Object with the following structure:
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Record {
private String tid;
private String eid;
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime ts;
public Record() {
}
}
When I try and convert this to a string using the following method,
public static String convertToJSON(Object object) {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json;
try {
json = ow.writeValueAsString(object);
LOGGER.info("(convertToJSON) JSON: {}", json);
} catch (Exception e) {
throw new JSONException("Error Converting JSON To string", e);
}
return json;
}
I get a comma separated value as below: "ts" : [ 2021, 4, 4, 20, 17, 50, 522000000 ]
I want this printed as ISO-UTC "ts" : "2021-04-04 20:46:44:932" or "ts" : "2021-04-04 20:46:44:932z".
One was would be to add @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss:SSS") to the record object but I would not prefer not changing the Json object definition as this has inflight data impact in production. Is there a way to change my convertToJSON method to achieve this.