1717package org .springframework .integration .support .json ;
1818
1919import java .io .File ;
20+ import java .io .IOException ;
2021import java .io .InputStream ;
2122import java .io .Reader ;
2223import java .io .Writer ;
2324import java .lang .reflect .Type ;
2425import java .net .URL ;
25- import java .util .ArrayList ;
2626import java .util .Collection ;
27- import java .util .List ;
2827import java .util .Map ;
2928
3029import tools .jackson .core .JacksonException ;
3130import tools .jackson .core .JsonParser ;
32- import tools .jackson .databind .JacksonModule ;
3331import tools .jackson .databind .JavaType ;
3432import tools .jackson .databind .JsonNode ;
3533import tools .jackson .databind .ObjectMapper ;
3634import tools .jackson .databind .json .JsonMapper ;
3735
3836import org .springframework .integration .mapping .support .JsonHeaders ;
3937import org .springframework .util .Assert ;
40- import org .springframework .util .ClassUtils ;
4138
4239/**
4340 * Jackson 3 JSON-processor (@link https://github.com/FasterXML)
5754 * @since 7.0
5855 *
5956 */
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 > {
6858
6959 private final ObjectMapper objectMapper ;
7060
71- public Jackson3JsonObjectMapper () {
72- List <JacksonModule > jacksonModules = collectWellKnownModulesIfAvailable ();
61+ public JacksonJsonObjectMapper () {
7362 this .objectMapper = JsonMapper .builder ()
74- .addModules ( jacksonModules )
63+ .findAndAddModules ( JacksonJsonObjectMapper . class . getClassLoader () )
7564 .build ();
7665 }
7766
78- public Jackson3JsonObjectMapper (ObjectMapper objectMapper ) {
67+ public JacksonJsonObjectMapper (ObjectMapper objectMapper ) {
7968 Assert .notNull (objectMapper , "objectMapper must not be null" );
8069 this .objectMapper = objectMapper ;
8170 }
@@ -85,17 +74,27 @@ public ObjectMapper getObjectMapper() {
8574 }
8675
8776 @ 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+ }
9084 }
9185
9286 @ 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+ }
9594 }
9695
9796 @ Override
98- public JsonNode toJsonNode (Object json ) throws JacksonException {
97+ public JsonNode toJsonNode (Object json ) throws IOException {
9998 try {
10099 if (json instanceof String ) {
101100 return this .objectMapper .readTree ((String ) json );
@@ -118,7 +117,7 @@ else if (json instanceof Reader) {
118117 }
119118 catch (JacksonException e ) {
120119 if (!(json instanceof String ) && !(json instanceof byte [])) {
121- throw e ;
120+ throw new IOException ( e ) ;
122121 }
123122 // Otherwise the input might not be valid JSON, fallback to TextNode with ObjectMapper.valueToTree()
124123 }
@@ -127,34 +126,44 @@ else if (json instanceof Reader) {
127126 }
128127
129128 @ 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+ }
148153 }
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 );
152156 }
153157 }
154158
155159 @ 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+ }
158167 }
159168
160169 @ Override
@@ -182,29 +191,4 @@ protected JavaType constructType(Type type) {
182191 return this .objectMapper .constructType (type );
183192 }
184193
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-
210194}
0 commit comments