Skip to content

Commit

Permalink
backport custom date serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
bou3108 committed Feb 9, 2025
1 parent 404c48c commit 9c9f5e7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
21 changes: 2 additions & 19 deletions src/main/java/com/hubsante/model/EdxlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,9 @@
@Slf4j
public class EdxlHandler {

public XmlMapper xmlMapper;
public XmlMapper xmlMapper = Utils.getXmlMapper();

public ObjectMapper jsonMapper;

public EdxlHandler() {
xmlMapper = (XmlMapper) new XmlMapper()
.registerModule(new JavaTimeModule())
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

xmlMapper.configure(com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);

jsonMapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
}
public ObjectMapper jsonMapper = Utils.getJsonMapper();

public EdxlMessage deserializeJsonEDXL(String json) throws JsonProcessingException {
return jsonMapper.readValue(json, EdxlMessage.class);
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/hubsante/model/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.hubsante.model;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Utils {

public static JavaTimeModule createCustomJavaTimeModule() {
JavaTimeModule javaTimeModule = new JavaTimeModule();

javaTimeModule.addSerializer(OffsetDateTime.class,
new com.fasterxml.jackson.databind.ser.std.StdSerializer<OffsetDateTime>(OffsetDateTime.class) {

@Override
public void serialize(OffsetDateTime value, com.fasterxml.jackson.core.JsonGenerator gen,
com.fasterxml.jackson.databind.SerializerProvider provider) throws IOException {
String formatted = value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"));
// Replace 'Z' with '+00:00' during serialization
if (formatted.endsWith("Z")) {
formatted = formatted.substring(0, formatted.length() - 1) + "+00:00";
}

gen.writeString(formatted);
}
}
);

return javaTimeModule;
}

public static XmlMapper getXmlMapper() {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper = (XmlMapper) new XmlMapper()
.registerModule(createCustomJavaTimeModule())
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

xmlMapper.configure(com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
return xmlMapper;
}

public static ObjectMapper getJsonMapper() {
ObjectMapper jsonMapper = new ObjectMapper()
.registerModule(createCustomJavaTimeModule())
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

return jsonMapper;
}
}

0 comments on commit 9c9f5e7

Please sign in to comment.