17
17
package org .springframework .integration .support .json ;
18
18
19
19
import java .io .File ;
20
+ import java .io .IOException ;
20
21
import java .io .InputStream ;
21
22
import java .io .Reader ;
22
23
import java .io .Writer ;
23
24
import java .lang .reflect .Type ;
24
25
import java .net .URL ;
25
- import java .util .ArrayList ;
26
26
import java .util .Collection ;
27
- import java .util .List ;
28
27
import java .util .Map ;
29
28
30
29
import tools .jackson .core .JacksonException ;
31
30
import tools .jackson .core .JsonParser ;
32
- import tools .jackson .databind .JacksonModule ;
33
31
import tools .jackson .databind .JavaType ;
34
32
import tools .jackson .databind .JsonNode ;
35
33
import tools .jackson .databind .ObjectMapper ;
36
34
import tools .jackson .databind .json .JsonMapper ;
37
35
38
36
import org .springframework .integration .mapping .support .JsonHeaders ;
39
37
import org .springframework .util .Assert ;
40
- import org .springframework .util .ClassUtils ;
41
38
42
39
/**
43
40
* Jackson 3 JSON-processor (@link https://github.com/FasterXML)
57
54
* @since 7.0
58
55
*
59
56
*/
60
- public class Jackson3JsonObjectMapper extends AbstractJacksonJsonObjectMapper <JsonNode , JsonParser , JavaType > {
61
-
62
- private static final boolean JODA_MODULE_PRESENT =
63
- ClassUtils .isPresent ("tools.jackson.datatype.joda.JodaModule" , null );
64
-
65
- private static final boolean KOTLIN_MODULE_PRESENT =
66
- ClassUtils .isPresent ("kotlin.Unit" , null ) &&
67
- ClassUtils .isPresent ("tools.jackson.module.kotlin.KotlinModule" , null );
57
+ public class JacksonJsonObjectMapper extends AbstractJacksonJsonObjectMapper <JsonNode , JsonParser , JavaType > {
68
58
69
59
private final ObjectMapper objectMapper ;
70
60
71
- public Jackson3JsonObjectMapper () {
72
- List <JacksonModule > jacksonModules = collectWellKnownModulesIfAvailable ();
61
+ public JacksonJsonObjectMapper () {
73
62
this .objectMapper = JsonMapper .builder ()
74
- .addModules ( jacksonModules )
63
+ .findAndAddModules ( JacksonJsonObjectMapper . class . getClassLoader () )
75
64
.build ();
76
65
}
77
66
78
- public Jackson3JsonObjectMapper (ObjectMapper objectMapper ) {
67
+ public JacksonJsonObjectMapper (ObjectMapper objectMapper ) {
79
68
Assert .notNull (objectMapper , "objectMapper must not be null" );
80
69
this .objectMapper = objectMapper ;
81
70
}
@@ -85,17 +74,27 @@ public ObjectMapper getObjectMapper() {
85
74
}
86
75
87
76
@ Override
88
- public String toJson (Object value ) throws JacksonException {
89
- return this .objectMapper .writeValueAsString (value );
77
+ public String toJson (Object value ) throws IOException {
78
+ try {
79
+ return this .objectMapper .writeValueAsString (value );
80
+ }
81
+ catch (JacksonException e ) {
82
+ throw new IOException (e );
83
+ }
90
84
}
91
85
92
86
@ Override
93
- public void toJson (Object value , Writer writer ) throws JacksonException {
94
- this .objectMapper .writeValue (writer , value );
87
+ public void toJson (Object value , Writer writer ) throws IOException {
88
+ try {
89
+ this .objectMapper .writeValue (writer , value );
90
+ }
91
+ catch (JacksonException e ) {
92
+ throw new IOException (e );
93
+ }
95
94
}
96
95
97
96
@ Override
98
- public JsonNode toJsonNode (Object json ) throws JacksonException {
97
+ public JsonNode toJsonNode (Object json ) throws IOException {
99
98
try {
100
99
if (json instanceof String ) {
101
100
return this .objectMapper .readTree ((String ) json );
@@ -118,7 +117,7 @@ else if (json instanceof Reader) {
118
117
}
119
118
catch (JacksonException e ) {
120
119
if (!(json instanceof String ) && !(json instanceof byte [])) {
121
- throw e ;
120
+ throw new IOException ( e ) ;
122
121
}
123
122
// Otherwise the input might not be valid JSON, fallback to TextNode with ObjectMapper.valueToTree()
124
123
}
@@ -127,34 +126,44 @@ else if (json instanceof Reader) {
127
126
}
128
127
129
128
@ Override
130
- protected <T > T fromJson (Object json , JavaType type ) throws RuntimeException {
131
- if (json instanceof String ) {
132
- return this .objectMapper .readValue ((String ) json , type );
133
- }
134
- else if (json instanceof byte []) {
135
- return this .objectMapper .readValue ((byte []) json , type );
136
- }
137
- else if (json instanceof File ) {
138
- return this .objectMapper .readValue ((File ) json , type );
139
- }
140
- else if (json instanceof URL ) {
141
- return this .objectMapper .readValue ((URL ) json , type );
142
- }
143
- else if (json instanceof InputStream ) {
144
- return this .objectMapper .readValue ((InputStream ) json , type );
145
- }
146
- else if (json instanceof Reader ) {
147
- return this .objectMapper .readValue ((Reader ) json , type );
129
+ protected <T > T fromJson (Object json , JavaType type ) throws IOException {
130
+ try {
131
+ if (json instanceof String ) {
132
+ return this .objectMapper .readValue ((String ) json , type );
133
+ }
134
+ else if (json instanceof byte []) {
135
+ return this .objectMapper .readValue ((byte []) json , type );
136
+ }
137
+ else if (json instanceof File ) {
138
+ return this .objectMapper .readValue ((File ) json , type );
139
+ }
140
+ else if (json instanceof URL ) {
141
+ return this .objectMapper .readValue ((URL ) json , type );
142
+ }
143
+ else if (json instanceof InputStream ) {
144
+ return this .objectMapper .readValue ((InputStream ) json , type );
145
+ }
146
+ else if (json instanceof Reader ) {
147
+ return this .objectMapper .readValue ((Reader ) json , type );
148
+ }
149
+ else {
150
+ throw new IllegalArgumentException ("'json' argument must be an instance of: " + SUPPORTED_JSON_TYPES
151
+ + " , but gotten: " + json .getClass ());
152
+ }
148
153
}
149
- else {
150
- throw new IllegalArgumentException ("'json' argument must be an instance of: " + SUPPORTED_JSON_TYPES
151
- + " , but gotten: " + json .getClass ());
154
+ catch (JacksonException e ) {
155
+ throw new IOException (e );
152
156
}
153
157
}
154
158
155
159
@ Override
156
- public <T > T fromJson (JsonParser parser , Type valueType ) throws JacksonException {
157
- return this .objectMapper .readValue (parser , constructType (valueType ));
160
+ public <T > T fromJson (JsonParser parser , Type valueType ) throws IOException {
161
+ try {
162
+ return this .objectMapper .readValue (parser , constructType (valueType ));
163
+ }
164
+ catch (JacksonException e ) {
165
+ throw new IOException (e );
166
+ }
158
167
}
159
168
160
169
@ Override
@@ -182,29 +191,4 @@ protected JavaType constructType(Type type) {
182
191
return this .objectMapper .constructType (type );
183
192
}
184
193
185
- private List <JacksonModule > collectWellKnownModulesIfAvailable () {
186
- List <JacksonModule > modules = new ArrayList <>();
187
- if (JODA_MODULE_PRESENT ) {
188
- modules .add (JodaModuleProvider .MODULE );
189
- }
190
- if (KOTLIN_MODULE_PRESENT ) {
191
- modules .add (KotlinModuleProvider .MODULE );
192
- }
193
- return modules ;
194
- }
195
-
196
- private static final class JodaModuleProvider {
197
-
198
- static final tools .jackson .databind .JacksonModule MODULE =
199
- new tools .jackson .datatype .joda .JodaModule ();
200
-
201
- }
202
-
203
- private static final class KotlinModuleProvider {
204
-
205
- static final tools .jackson .databind .JacksonModule MODULE =
206
- new tools .jackson .module .kotlin .KotlinModule .Builder ().build ();
207
-
208
- }
209
-
210
194
}
0 commit comments