Skip to content

Commit 7fbb92f

Browse files
Add LegacyDateDeserializer for handling legacy date formats and update BasicEntityJsonDeserializer constructor
1 parent 4c69378 commit 7fbb92f

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tools.dynamia.commons;
2+
3+
4+
import tools.jackson.core.JsonParser;
5+
import tools.jackson.databind.DeserializationContext;
6+
import tools.jackson.databind.deser.std.StdDeserializer;
7+
8+
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
10+
import java.time.format.DateTimeFormatter;
11+
12+
/**
13+
* Custom deserializer to handle legacy date formats in JSON. It attempts to parse the date string using two patterns:
14+
* 1. "yyyy-MM-dd HH:mm:ss" - for date-time strings that include both date and time.
15+
* 2. "yyyy-MM-dd" - for date-only strings.
16+
* @author Mario A. Serrano Leones
17+
*/
18+
public class LegacyDateDeserializer extends StdDeserializer<LocalDate> {
19+
20+
public static final String DATE_PATTERN = "yyyy-MM-dd";
21+
public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
22+
23+
public LegacyDateDeserializer() {
24+
super(LocalDate.class);
25+
}
26+
27+
@Override
28+
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) {
29+
30+
String value = p.getString();
31+
32+
if (value == null || value.isBlank()) {
33+
return null;
34+
}
35+
36+
37+
try {
38+
return LocalDateTime.parse(value, DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)).toLocalDate();
39+
} catch (Exception e) {
40+
41+
}
42+
43+
44+
return LocalDate.parse(value, DateTimeFormatter.ofPattern(DATE_PATTERN));
45+
46+
47+
}
48+
}

platform/core/commons/src/main/java/tools/dynamia/commons/StringPojoParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import tools.jackson.core.JacksonException;
2222
import tools.jackson.core.type.TypeReference;
23+
import tools.jackson.databind.DeserializationFeature;
2324
import tools.jackson.databind.JavaType;
2425
import tools.jackson.databind.SerializationFeature;
2526
import tools.jackson.databind.json.JsonMapper;
@@ -68,6 +69,7 @@ public static JsonMapper createJsonMapper() {
6869
return JsonMapper.builder()
6970
.enable(SerializationFeature.INDENT_OUTPUT)
7071
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
72+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
7173
.build();
7274

7375
}
@@ -130,7 +132,6 @@ public static Map<String, String> parseJsonToStringMap(String json) {
130132
*
131133
* @param json
132134
* @param pojoType
133-
*
134135
* @return object of type or null if json is null or empty
135136
*/
136137
public static <T> T parseJsonToPojo(String json, Class<T> pojoType) {
@@ -149,6 +150,7 @@ public static <T> T parseJsonToPojo(String json, Class<T> pojoType) {
149150

150151
/**
151152
* Parse JSON map to java type (java bean)
153+
*
152154
* @param map
153155
* @param pojoType
154156
* @return object of type or null if json is null or empty
@@ -184,7 +186,6 @@ public static String convertPojoToXml(Object pojo) {
184186
/**
185187
* Create a xml {@link XmlMapper} with enable IDENT_OUTPUT and disabled FAIL_ON_EMPTY_BEANS. Also add support
186188
*
187-
*
188189
* @return xml mapper
189190
*/
190191
public static XmlMapper createXmlMapper() {

platform/core/domain/src/main/java/tools/dynamia/domain/util/BasicEntityJsonDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class BasicEntityJsonDeserializer extends StdDeserializer<AbstractEntity> {
1414

1515
public BasicEntityJsonDeserializer() {
16-
this(null);
16+
this(AbstractEntity.class);
1717
}
1818

1919
public BasicEntityJsonDeserializer(Class<?> vc) {

0 commit comments

Comments
 (0)