|
1 | 1 | package hirez.api.object.adapters; |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.core.JsonParseException; |
4 | 3 | import com.fasterxml.jackson.core.JsonParser; |
5 | 4 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.core.JsonToken; |
6 | 6 | import com.fasterxml.jackson.databind.BeanProperty; |
7 | 7 | import com.fasterxml.jackson.databind.DeserializationContext; |
8 | 8 | import com.fasterxml.jackson.databind.JsonDeserializer; |
9 | 9 | import com.fasterxml.jackson.databind.JsonMappingException; |
10 | 10 | import com.fasterxml.jackson.databind.deser.ContextualDeserializer; |
11 | 11 |
|
12 | 12 | import java.io.IOException; |
13 | | -import java.text.ParseException; |
14 | | -import java.text.SimpleDateFormat; |
| 13 | +import java.time.Instant; |
| 14 | +import java.time.ZoneId; |
15 | 15 | import java.time.ZoneOffset; |
| 16 | +import java.time.format.DateTimeFormatter; |
16 | 17 | import java.util.Date; |
17 | | -import java.util.TimeZone; |
| 18 | +import java.util.Locale; |
18 | 19 |
|
19 | 20 | public class DateTimeDeserializer extends JsonDeserializer<Date> implements ContextualDeserializer { |
20 | 21 |
|
21 | 22 | private DateTimeFormat annotationDTF; |
22 | 23 |
|
23 | 24 | @Override |
24 | 25 | public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { |
25 | | - SimpleDateFormat sdf = new SimpleDateFormat(annotationDTF.value()); |
26 | | - sdf.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC)); |
27 | | - |
28 | | - try { |
29 | | - return sdf.parse(p.getText()); |
30 | | - } catch (ParseException e) { |
31 | | - throw new JsonParseException(p, "Cannot parse to Date format!", e); |
32 | | - } |
| 26 | + if (p.hasToken(JsonToken.VALUE_NULL)) return null; |
| 27 | + String text = p.getText(); |
| 28 | + if (text.isEmpty()) return null; |
| 29 | + |
| 30 | + return Date.from(DateTimeFormatter.ofPattern(annotationDTF.value(), Locale.US) |
| 31 | + .withZone(ZoneId.from(ZoneOffset.UTC)) |
| 32 | + .parse(text, Instant::from)); |
33 | 33 | } |
34 | 34 |
|
35 | 35 | @Override |
|
0 commit comments