1 package top.infra.jackson2;
2
3 import static java.lang.Boolean.FALSE;
4
5 import com.fasterxml.jackson.databind.ObjectMapper;
6
7 import org.springframework.core.Ordered;
8
9 import java.util.Optional;
10 import java.util.TimeZone;
11
12 import top.infra.common.ClassUtils;
13
14
15
16
17 public interface Jackson2MapperCustomizer extends Ordered {
18
19 String CLASS_XML_MAPPER = "com.fasterxml.jackson.dataformat.xml.XmlMapper";
20
21 String PATTERN_JAVA_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
22
23 void customize(Jackson2Properties properties, ObjectMapper mapper);
24
25 @Override
26 default int getOrder() {
27 return Ordered.LOWEST_PRECEDENCE;
28 }
29
30 default String dateFormat(final Jackson2Properties properties) {
31 final String dateFormat;
32 if (properties.getDateFormat() == null) {
33 dateFormat = PATTERN_JAVA_ISO8601;
34 } else {
35 dateFormat = properties.getDateFormat();
36 }
37 return dateFormat;
38 }
39
40 default Boolean isXmlMapper(final ObjectMapper mapper) {
41 final Optional<Class<?>> classOptional = ClassUtils.forName(CLASS_XML_MAPPER);
42 return mapper != null && classOptional.map(xmlMapperClass -> xmlMapperClass.isAssignableFrom(mapper.getClass())).orElse(FALSE);
43 }
44
45 default TimeZone timeZone(final Jackson2Properties properties) {
46
47
48
49 final TimeZone timeZone;
50 if (properties.getTimeZone() == null) {
51
52 timeZone = new ObjectMapper().getSerializationConfig().getTimeZone();
53 } else {
54 timeZone = properties.getTimeZone();
55 }
56 return timeZone;
57 }
58 }