1
1
/*
2
- * Copyright 2019 the original author or authors.
2
+ * Copyright 2019-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .kafka .support ;
18
18
19
- import org .springframework .beans .BeanUtils ;
20
19
import org .springframework .util .ClassUtils ;
21
20
22
21
import com .fasterxml .jackson .databind .DeserializationFeature ;
23
22
import com .fasterxml .jackson .databind .MapperFeature ;
24
- import com .fasterxml .jackson .databind .Module ;
25
23
import com .fasterxml .jackson .databind .ObjectMapper ;
26
24
import com .fasterxml .jackson .databind .json .JsonMapper ;
27
25
34
32
*/
35
33
public final class JacksonUtils {
36
34
37
- private static final String UNUSED = "unused" ;
35
+ private static final boolean JDK8_MODULE_PRESENT =
36
+ ClassUtils .isPresent ("com.fasterxml.jackson.datatype.jdk8.Jdk8Module" , null );
37
+
38
+ private static final boolean JAVA_TIME_MODULE_PRESENT =
39
+ ClassUtils .isPresent ("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule" , null );
40
+
41
+ private static final boolean JODA_MODULE_PRESENT =
42
+ ClassUtils .isPresent ("com.fasterxml.jackson.datatype.joda.JodaModule" , null );
43
+
44
+ private static final boolean KOTLIN_MODULE_PRESENT =
45
+ ClassUtils .isPresent ("kotlin.Unit" , null ) &&
46
+ ClassUtils .isPresent ("com.fasterxml.jackson.module.kotlin.KotlinModule" , null );
38
47
39
48
/**
40
49
* Factory for {@link ObjectMapper} instances with registered well-known modules
41
50
* and disabled {@link MapperFeature#DEFAULT_VIEW_INCLUSION} and
42
51
* {@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} features.
43
- * The {@link ClassUtils#getDefaultClassLoader()} is used for loading module classes.
44
52
* @return the {@link ObjectMapper} instance.
45
53
*/
46
54
public static ObjectMapper enhancedObjectMapper () {
47
- return enhancedObjectMapper (ClassUtils .getDefaultClassLoader ());
55
+ ObjectMapper objectMapper = JsonMapper .builder ()
56
+ .configure (MapperFeature .DEFAULT_VIEW_INCLUSION , false )
57
+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
58
+ .build ();
59
+ registerWellKnownModulesIfAvailable (objectMapper );
60
+ return objectMapper ;
48
61
}
49
62
50
63
/**
@@ -53,63 +66,61 @@ public static ObjectMapper enhancedObjectMapper() {
53
66
* {@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} features.
54
67
* @param classLoader the {@link ClassLoader} for modules to register.
55
68
* @return the {@link ObjectMapper} instance.
69
+ * @deprecated since 2.7.5 in favor of {@link #enhancedObjectMapper()}
56
70
*/
71
+ @ Deprecated
57
72
public static ObjectMapper enhancedObjectMapper (ClassLoader classLoader ) {
58
- ObjectMapper objectMapper = JsonMapper .builder ()
59
- .configure (MapperFeature .DEFAULT_VIEW_INCLUSION , false )
60
- .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
61
- .build ();
62
- registerWellKnownModulesIfAvailable (objectMapper , classLoader );
63
- return objectMapper ;
73
+ return enhancedObjectMapper ();
64
74
}
65
75
66
- @ SuppressWarnings ("unchecked" )
67
- private static void registerWellKnownModulesIfAvailable (ObjectMapper objectMapper , ClassLoader classLoader ) {
76
+ private static void registerWellKnownModulesIfAvailable (ObjectMapper objectMapper ) {
68
77
objectMapper .registerModule (new JacksonMimeTypeModule ());
69
- try {
70
- Class <? extends Module > jdk8Module = (Class <? extends Module >)
71
- ClassUtils .forName ("com.fasterxml.jackson.datatype.jdk8.Jdk8Module" , classLoader );
72
- objectMapper .registerModule (BeanUtils .instantiateClass (jdk8Module ));
73
- }
74
- catch (@ SuppressWarnings (UNUSED ) ClassNotFoundException ex ) {
75
- // jackson-datatype-jdk8 not available
78
+ if (JDK8_MODULE_PRESENT ) {
79
+ objectMapper .registerModule (Jdk8ModuleProvider .MODULE );
76
80
}
77
81
78
- try {
79
- Class <? extends Module > javaTimeModule = (Class <? extends Module >)
80
- ClassUtils .forName ("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule" , classLoader );
81
- objectMapper .registerModule (BeanUtils .instantiateClass (javaTimeModule ));
82
- }
83
- catch (@ SuppressWarnings (UNUSED ) ClassNotFoundException ex ) {
84
- // jackson-datatype-jsr310 not available
82
+ if (JAVA_TIME_MODULE_PRESENT ) {
83
+ objectMapper .registerModule (JavaTimeModuleProvider .MODULE );
85
84
}
86
85
87
- // Joda-Time present?
88
- if (ClassUtils .isPresent ("org.joda.time.LocalDate" , classLoader )) {
89
- try {
90
- Class <? extends Module > jodaModule = (Class <? extends Module >)
91
- ClassUtils .forName ("com.fasterxml.jackson.datatype.joda.JodaModule" , classLoader );
92
- objectMapper .registerModule (BeanUtils .instantiateClass (jodaModule ));
93
- }
94
- catch (@ SuppressWarnings (UNUSED ) ClassNotFoundException ex ) {
95
- // jackson-datatype-joda not available
96
- }
86
+ if (JODA_MODULE_PRESENT ) {
87
+ objectMapper .registerModule (JodaModuleProvider .MODULE );
97
88
}
98
89
99
- // Kotlin present?
100
- if (ClassUtils .isPresent ("kotlin.Unit" , classLoader )) {
101
- try {
102
- Class <? extends Module > kotlinModule = (Class <? extends Module >)
103
- ClassUtils .forName ("com.fasterxml.jackson.module.kotlin.KotlinModule" , classLoader );
104
- objectMapper .registerModule (BeanUtils .instantiateClass (kotlinModule ));
105
- }
106
- catch (@ SuppressWarnings (UNUSED ) ClassNotFoundException ex ) {
107
- //jackson-module-kotlin not available
108
- }
90
+ if (KOTLIN_MODULE_PRESENT ) {
91
+ objectMapper .registerModule (KotlinModuleProvider .MODULE );
109
92
}
110
93
}
111
94
112
95
private JacksonUtils () {
113
96
}
114
97
98
+ private static final class Jdk8ModuleProvider {
99
+
100
+ static final com .fasterxml .jackson .databind .Module MODULE =
101
+ new com .fasterxml .jackson .datatype .jdk8 .Jdk8Module ();
102
+
103
+ }
104
+
105
+ private static final class JavaTimeModuleProvider {
106
+
107
+ static final com .fasterxml .jackson .databind .Module MODULE =
108
+ new com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ();
109
+
110
+ }
111
+
112
+ private static final class JodaModuleProvider {
113
+
114
+ static final com .fasterxml .jackson .databind .Module MODULE =
115
+ new com .fasterxml .jackson .datatype .joda .JodaModule ();
116
+
117
+ }
118
+
119
+ private static final class KotlinModuleProvider {
120
+
121
+ static final com .fasterxml .jackson .databind .Module MODULE =
122
+ new com .fasterxml .jackson .module .kotlin .KotlinModule ();
123
+
124
+ }
125
+
115
126
}
0 commit comments