Skip to content

Commit aed23a3

Browse files
committed
Rename Jackson3 classes and handle exception
* Rename `Jackson3JsonObjectMapper` to `JacksonJsonObjectMapper` * Rename `Jackson3JsonMessageParser` to `JacksonJsonMessageParser` * Wrap `JacksonException` in `IOException` to maintain API contract * Replace manual module collection with `findAndAddModules()` Signed-off-by: Jooyoung Pyoung <[email protected]>
1 parent 6926b29 commit aed23a3

File tree

4 files changed

+366
-77
lines changed

4 files changed

+366
-77
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,26 @@
3838
*
3939
* @since 7.0
4040
*/
41-
public class Jackson3JsonMessageParser extends AbstractJacksonJsonMessageParser<JsonParser> {
41+
public class JacksonJsonMessageParser extends AbstractJacksonJsonMessageParser<JsonParser> {
42+
4243
private static final JsonFactory JSON_FACTORY = JsonFactory.builder().build();
4344

44-
public Jackson3JsonMessageParser() {
45-
this(new Jackson3JsonObjectMapper());
45+
public JacksonJsonMessageParser() {
46+
this(new JacksonJsonObjectMapper());
4647
}
4748

48-
public Jackson3JsonMessageParser(Jackson3JsonObjectMapper objectMapper) {
49+
public JacksonJsonMessageParser(JacksonJsonObjectMapper objectMapper) {
4950
super(objectMapper);
5051
}
5152

5253
@Override
53-
protected JsonParser createJsonParser(String jsonMessage) throws JacksonException {
54+
protected JsonParser createJsonParser(String jsonMessage) {
5455
return JSON_FACTORY.createParser(ObjectReadContext.empty(), jsonMessage);
5556
}
5657

5758
@Override
5859
protected Message<?> parseWithHeaders(JsonParser parser, String jsonMessage,
59-
@Nullable Map<String, Object> headersToAdd) throws JacksonException {
60+
@Nullable Map<String, Object> headersToAdd) {
6061

6162
String error = AbstractJsonInboundMessageMapper.MESSAGE_FORMAT_ERROR + jsonMessage;
6263
Assert.isTrue(JsonToken.START_OBJECT == parser.nextToken(), error);
Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,24 @@
1717
package org.springframework.integration.support.json;
1818

1919
import java.io.File;
20+
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.io.Reader;
2223
import java.io.Writer;
2324
import java.lang.reflect.Type;
2425
import java.net.URL;
25-
import java.util.ArrayList;
2626
import java.util.Collection;
27-
import java.util.List;
2827
import java.util.Map;
2928

3029
import tools.jackson.core.JacksonException;
3130
import tools.jackson.core.JsonParser;
32-
import tools.jackson.databind.JacksonModule;
3331
import tools.jackson.databind.JavaType;
3432
import tools.jackson.databind.JsonNode;
3533
import tools.jackson.databind.ObjectMapper;
3634
import tools.jackson.databind.json.JsonMapper;
3735

3836
import org.springframework.integration.mapping.support.JsonHeaders;
3937
import org.springframework.util.Assert;
40-
import org.springframework.util.ClassUtils;
4138

4239
/**
4340
* Jackson 3 JSON-processor (@link https://github.com/FasterXML)
@@ -57,25 +54,17 @@
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
}

spring-integration-core/src/main/java/org/springframework/integration/support/json/JsonObjectMapperProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private JsonObjectMapperProvider() {
4545
return new Jackson2JsonObjectMapper();
4646
}
4747
else if (JacksonPresent.isJackson3Present()) {
48-
return new Jackson3JsonObjectMapper();
48+
return new JacksonJsonObjectMapper();
4949
}
5050
else {
5151
throw new IllegalStateException("No jackson-databind.jar is present in the classpath.");

0 commit comments

Comments
 (0)