|
| 1 | +/* |
| 2 | + * Copyright 2025-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.support.json; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.Reader; |
| 22 | +import java.io.Writer; |
| 23 | +import java.lang.reflect.Type; |
| 24 | +import java.net.URL; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import tools.jackson.core.JacksonException; |
| 31 | +import tools.jackson.core.JsonParser; |
| 32 | +import tools.jackson.databind.JacksonModule; |
| 33 | +import tools.jackson.databind.JavaType; |
| 34 | +import tools.jackson.databind.JsonNode; |
| 35 | +import tools.jackson.databind.ObjectMapper; |
| 36 | +import tools.jackson.databind.json.JsonMapper; |
| 37 | + |
| 38 | +import org.springframework.integration.mapping.support.JsonHeaders; |
| 39 | +import org.springframework.util.Assert; |
| 40 | +import org.springframework.util.ClassUtils; |
| 41 | + |
| 42 | +/** |
| 43 | + * Jackson 3 JSON-processor (@link https://github.com/FasterXML) |
| 44 | + * {@linkplain JsonObjectMapper} implementation. |
| 45 | + * Delegates {@link #toJson} and {@link #fromJson} |
| 46 | + * to the {@linkplain ObjectMapper} |
| 47 | + * <p> |
| 48 | + * It customizes Jackson's default properties with the following ones: |
| 49 | + * <ul> |
| 50 | + * <li>The well-known modules are registered through the classpath scan</li> |
| 51 | + * </ul> |
| 52 | + * |
| 53 | + * See {@code tools.jackson.databind.json.JsonMapper.builder} for more information. |
| 54 | + * |
| 55 | + * @author Jooyoung Pyoung |
| 56 | + * |
| 57 | + * @since 7.0 |
| 58 | + * |
| 59 | + */ |
| 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); |
| 68 | + |
| 69 | + private final ObjectMapper objectMapper; |
| 70 | + |
| 71 | + public Jackson3JsonObjectMapper() { |
| 72 | + List<JacksonModule> jacksonModules = collectWellKnownModulesIfAvailable(); |
| 73 | + this.objectMapper = JsonMapper.builder() |
| 74 | + .addModules(jacksonModules) |
| 75 | + .build(); |
| 76 | + } |
| 77 | + |
| 78 | + public Jackson3JsonObjectMapper(ObjectMapper objectMapper) { |
| 79 | + Assert.notNull(objectMapper, "objectMapper must not be null"); |
| 80 | + this.objectMapper = objectMapper; |
| 81 | + } |
| 82 | + |
| 83 | + public ObjectMapper getObjectMapper() { |
| 84 | + return this.objectMapper; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String toJson(Object value) throws JacksonException { |
| 89 | + return this.objectMapper.writeValueAsString(value); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void toJson(Object value, Writer writer) throws JacksonException { |
| 94 | + this.objectMapper.writeValue(writer, value); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public JsonNode toJsonNode(Object json) throws JacksonException { |
| 99 | + try { |
| 100 | + if (json instanceof String) { |
| 101 | + return this.objectMapper.readTree((String) json); |
| 102 | + } |
| 103 | + else if (json instanceof byte[]) { |
| 104 | + return this.objectMapper.readTree((byte[]) json); |
| 105 | + } |
| 106 | + else if (json instanceof File) { |
| 107 | + return this.objectMapper.readTree((File) json); |
| 108 | + } |
| 109 | + else if (json instanceof URL) { |
| 110 | + return this.objectMapper.readTree((URL) json); |
| 111 | + } |
| 112 | + else if (json instanceof InputStream) { |
| 113 | + return this.objectMapper.readTree((InputStream) json); |
| 114 | + } |
| 115 | + else if (json instanceof Reader) { |
| 116 | + return this.objectMapper.readTree((Reader) json); |
| 117 | + } |
| 118 | + } |
| 119 | + catch (JacksonException e) { |
| 120 | + if (!(json instanceof String) && !(json instanceof byte[])) { |
| 121 | + throw e; |
| 122 | + } |
| 123 | + // Otherwise the input might not be valid JSON, fallback to TextNode with ObjectMapper.valueToTree() |
| 124 | + } |
| 125 | + |
| 126 | + return this.objectMapper.valueToTree(json); |
| 127 | + } |
| 128 | + |
| 129 | + @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); |
| 148 | + } |
| 149 | + else { |
| 150 | + throw new IllegalArgumentException("'json' argument must be an instance of: " + SUPPORTED_JSON_TYPES |
| 151 | + + " , but gotten: " + json.getClass()); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public <T> T fromJson(JsonParser parser, Type valueType) throws JacksonException { |
| 157 | + return this.objectMapper.readValue(parser, constructType(valueType)); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + @SuppressWarnings({"unchecked"}) |
| 162 | + protected JavaType extractJavaType(Map<String, Object> javaTypes) { |
| 163 | + JavaType classType = this.createJavaType(javaTypes, JsonHeaders.TYPE_ID); |
| 164 | + if (!classType.isContainerType() || classType.isArrayType()) { |
| 165 | + return classType; |
| 166 | + } |
| 167 | + |
| 168 | + JavaType contentClassType = this.createJavaType(javaTypes, JsonHeaders.CONTENT_TYPE_ID); |
| 169 | + if (classType.getKeyType() == null) { |
| 170 | + return this.objectMapper.getTypeFactory() |
| 171 | + .constructCollectionType((Class<? extends Collection<?>>) classType.getRawClass(), |
| 172 | + contentClassType); |
| 173 | + } |
| 174 | + |
| 175 | + JavaType keyClassType = createJavaType(javaTypes, JsonHeaders.KEY_TYPE_ID); |
| 176 | + return this.objectMapper.getTypeFactory() |
| 177 | + .constructMapType((Class<? extends Map<?, ?>>) classType.getRawClass(), keyClassType, contentClassType); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + protected JavaType constructType(Type type) { |
| 182 | + return this.objectMapper.constructType(type); |
| 183 | + } |
| 184 | + |
| 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 | +} |
0 commit comments