|
| 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.ByteArrayInputStream; |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.io.Reader; |
| 24 | +import java.io.StringReader; |
| 25 | +import java.io.StringWriter; |
| 26 | +import java.net.URL; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.time.LocalDateTime; |
| 31 | +import java.time.ZoneId; |
| 32 | +import java.time.ZonedDateTime; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Date; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.Optional; |
| 40 | +import java.util.Set; |
| 41 | +import java.util.stream.Collectors; |
| 42 | + |
| 43 | +import org.joda.time.DateTime; |
| 44 | +import org.joda.time.DateTimeZone; |
| 45 | +import org.junit.jupiter.api.BeforeEach; |
| 46 | +import org.junit.jupiter.api.Test; |
| 47 | +import org.junit.jupiter.params.ParameterizedTest; |
| 48 | +import org.junit.jupiter.params.provider.ValueSource; |
| 49 | +import tools.jackson.databind.JacksonModule; |
| 50 | +import tools.jackson.databind.JsonNode; |
| 51 | +import tools.jackson.databind.ObjectMapper; |
| 52 | +import tools.jackson.databind.json.JsonMapper; |
| 53 | +import tools.jackson.datatype.joda.JodaModule; |
| 54 | +import tools.jackson.module.kotlin.KotlinModule; |
| 55 | + |
| 56 | +import org.springframework.util.ClassUtils; |
| 57 | + |
| 58 | +import static org.assertj.core.api.Assertions.assertThat; |
| 59 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 60 | + |
| 61 | +/** |
| 62 | + * @author Jooyoung Pyoung |
| 63 | + * |
| 64 | + * @since 7.0 |
| 65 | + */ |
| 66 | +class JacksonJsonObjectMapperTest { |
| 67 | + |
| 68 | + private static final boolean JODA_MODULE_PRESENT = |
| 69 | + ClassUtils.isPresent("tools.jackson.datatype.joda.JodaModule", null); |
| 70 | + |
| 71 | + private static final boolean KOTLIN_MODULE_PRESENT = |
| 72 | + ClassUtils.isPresent("kotlin.Unit", null) && |
| 73 | + ClassUtils.isPresent("tools.jackson.module.kotlin.KotlinModule", null); |
| 74 | + |
| 75 | + private JacksonJsonObjectMapper mapper; |
| 76 | + |
| 77 | + @BeforeEach |
| 78 | + void setUp() { |
| 79 | + mapper = new JacksonJsonObjectMapper(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void compareAutoDiscoveryVsManualModules() { |
| 84 | + ObjectMapper manualMapper = JsonMapper.builder() |
| 85 | + .addModules(collectWellKnownModulesIfAvailable()) |
| 86 | + .build(); |
| 87 | + |
| 88 | + Set<String> collectedModuleNames = getModuleNames(collectWellKnownModulesIfAvailable()); |
| 89 | + |
| 90 | + Set<String> autoModuleNames = getModuleNames(mapper.getObjectMapper().getRegisteredModules()); |
| 91 | + assertThat(autoModuleNames).isEqualTo(collectedModuleNames); |
| 92 | + |
| 93 | + Set<String> manualModuleNames = getModuleNames(manualMapper.getRegisteredModules()); |
| 94 | + assertThat(manualModuleNames).isEqualTo(collectedModuleNames); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testToJsonNodeWithVariousInputTypes() throws IOException { |
| 99 | + String jsonString = "{\"name\":\"test\",\"value\":123}"; |
| 100 | + JsonNode nodeFromString = mapper.toJsonNode(jsonString); |
| 101 | + assertThat(nodeFromString.get("name").asString()).isEqualTo("test"); |
| 102 | + assertThat(nodeFromString.get("value").asInt()).isEqualTo(123); |
| 103 | + |
| 104 | + byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8); |
| 105 | + JsonNode nodeFromBytes = mapper.toJsonNode(jsonBytes); |
| 106 | + assertThat(nodeFromBytes).isEqualTo(nodeFromString); |
| 107 | + |
| 108 | + try (InputStream inputStream = new ByteArrayInputStream(jsonBytes)) { |
| 109 | + JsonNode nodeFromInputStream = mapper.toJsonNode(inputStream); |
| 110 | + assertThat(nodeFromInputStream).isEqualTo(nodeFromString); |
| 111 | + } |
| 112 | + |
| 113 | + try (Reader reader = new StringReader(jsonString)) { |
| 114 | + JsonNode nodeFromReader = mapper.toJsonNode(reader); |
| 115 | + assertThat(nodeFromReader).isEqualTo(nodeFromString); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testToJsonNodeWithFile() throws IOException { |
| 121 | + Path tempFile = Files.createTempFile("test", ".json"); |
| 122 | + String jsonContent = "{\"message\":\"hello from file\",\"number\":42}"; |
| 123 | + Files.write(tempFile, jsonContent.getBytes(StandardCharsets.UTF_8)); |
| 124 | + |
| 125 | + try { |
| 126 | + File file = tempFile.toFile(); |
| 127 | + JsonNode nodeFromFile = mapper.toJsonNode(file); |
| 128 | + assertThat(nodeFromFile.get("message").asString()).isEqualTo("hello from file"); |
| 129 | + assertThat(nodeFromFile.get("number").asInt()).isEqualTo(42); |
| 130 | + |
| 131 | + URL fileUrl = file.toURI().toURL(); |
| 132 | + JsonNode nodeFromUrl = mapper.toJsonNode(fileUrl); |
| 133 | + assertThat(nodeFromUrl).isEqualTo(nodeFromFile); |
| 134 | + } |
| 135 | + finally { |
| 136 | + Files.deleteIfExists(tempFile); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testToJsonWithWriter() throws IOException { |
| 142 | + TestData data = new TestData("John", Optional.of("john@test.com"), Optional.empty()); |
| 143 | + |
| 144 | + try (StringWriter writer = new StringWriter()) { |
| 145 | + mapper.toJson(data, writer); |
| 146 | + String json = writer.toString(); |
| 147 | + assertThat(json).isEqualTo("{\"name\":\"John\",\"email\":\"john@test.com\",\"age\":null}"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void testFromJsonWithUnsupportedType() { |
| 153 | + Object unsupportedInput = new Date(); |
| 154 | + |
| 155 | + assertThatThrownBy(() -> mapper.fromJson(unsupportedInput, String.class)) |
| 156 | + .isInstanceOf(IllegalArgumentException.class); |
| 157 | + } |
| 158 | + |
| 159 | + @ParameterizedTest |
| 160 | + @ValueSource(strings = {"42", "true", "\"hello\"", "3.14", "null"}) |
| 161 | + void testPrimitiveTypes(String jsonValue) throws IOException { |
| 162 | + JsonNode node = mapper.toJsonNode(jsonValue); |
| 163 | + assertThat(node).isNotNull(); |
| 164 | + |
| 165 | + String serialized = mapper.toJson(node); |
| 166 | + JsonNode roundTrip = mapper.toJsonNode(serialized); |
| 167 | + assertThat(roundTrip).isEqualTo(node); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void testCollectionTypes() throws IOException { |
| 172 | + List<String> stringList = Arrays.asList("a", "b", "c"); |
| 173 | + String json = mapper.toJson(stringList); |
| 174 | + assertThat(json).isEqualTo("[\"a\",\"b\",\"c\"]"); |
| 175 | + |
| 176 | + @SuppressWarnings("unchecked") |
| 177 | + List<String> deserialized = mapper.fromJson(json, List.class); |
| 178 | + assertThat(deserialized).isEqualTo(stringList); |
| 179 | + |
| 180 | + Set<Integer> intSet = Set.of(1, 2, 3); |
| 181 | + String setJson = mapper.toJson(intSet); |
| 182 | + assertThat(setJson).isNotNull(); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + void testMapTypes() throws IOException { |
| 187 | + Map<String, Object> map = Map.of( |
| 188 | + "string", "value", |
| 189 | + "number", 42, |
| 190 | + "boolean", true, |
| 191 | + "nested", Map.of("inner", "value") |
| 192 | + ); |
| 193 | + |
| 194 | + String json = mapper.toJson(map); |
| 195 | + assertThat(json).isNotNull(); |
| 196 | + |
| 197 | + @SuppressWarnings("unchecked") |
| 198 | + Map<String, Object> deserialized = mapper.fromJson(json, Map.class); |
| 199 | + assertThat(deserialized.get("string")).isEqualTo("value"); |
| 200 | + assertThat(deserialized.get("number")).isEqualTo(42); |
| 201 | + assertThat(deserialized.get("boolean")).isEqualTo(true); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void testOptional() throws IOException { |
| 206 | + TestData data = new TestData("John", Optional.of("john@test.com"), Optional.empty()); |
| 207 | + |
| 208 | + String json = mapper.toJson(data); |
| 209 | + assertThat(json).isEqualTo("{\"name\":\"John\",\"email\":\"john@test.com\",\"age\":null}"); |
| 210 | + |
| 211 | + TestData deserialized = mapper.fromJson(json, TestData.class); |
| 212 | + assertThat(deserialized).isEqualTo(data); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testJavaTime() throws Exception { |
| 217 | + LocalDateTime localDateTime = LocalDateTime.of(2000, 1, 1, 0, 0); |
| 218 | + ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC")); |
| 219 | + TimeData data = new TimeData(localDateTime, zonedDateTime); |
| 220 | + |
| 221 | + String json = mapper.toJson(data); |
| 222 | + assertThat("{\"localDate\":\"2000-01-01T00:00:00\",\"zoneDate\":\"2000-01-01T00:00:00Z\"}").isEqualTo(json); |
| 223 | + |
| 224 | + // 물어보기 |
| 225 | + TimeData deserialized = mapper.fromJson(json, TimeData.class); |
| 226 | + assertThat(deserialized.localDate()).isEqualTo(data.localDate()); |
| 227 | + assertThat(deserialized.zoneDate().toInstant()).isEqualTo(data.zoneDate().toInstant()); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void testJodaWithJodaModule() throws Exception { |
| 232 | + ObjectMapper objectMapper = mapper.getObjectMapper(); |
| 233 | + Set<String> registeredModules = getModuleNames(objectMapper.getRegisteredModules()); |
| 234 | + assertThat(registeredModules.contains(JodaModuleProvider.MODULE.getModuleName())).isTrue(); |
| 235 | + |
| 236 | + org.joda.time.DateTime jodaDateTime = new DateTime(2000, 1, 1, 0, 0, DateTimeZone.UTC); |
| 237 | + JodaData data = new JodaData("John", jodaDateTime); |
| 238 | + |
| 239 | + String json = mapper.toJson(data); |
| 240 | + assertThat("{\"name\":\"John\",\"jodaDate\":\"2000-01-01T00:00:00.000Z\"}").isEqualTo(json); |
| 241 | + |
| 242 | + JodaData deserialized = mapper.fromJson(json, JodaData.class); |
| 243 | + assertThat(deserialized.name()).isEqualTo(data.name()); |
| 244 | + assertThat(deserialized.jodaDate()).isEqualTo(data.jodaDate()); |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + public void testJodaWithoutJodaModule() { |
| 249 | + ObjectMapper customMapper = JsonMapper.builder().build(); |
| 250 | + JacksonJsonObjectMapper mapper = new JacksonJsonObjectMapper(customMapper); |
| 251 | + |
| 252 | + Set<String> registeredModules = getModuleNames(mapper.getObjectMapper().getRegisteredModules()); |
| 253 | + assertThat(registeredModules.contains(JodaModuleProvider.MODULE.getModuleName())).isFalse(); |
| 254 | + |
| 255 | + org.joda.time.DateTime jodaDateTime = new DateTime(2000, 1, 1, 0, 0, DateTimeZone.UTC); |
| 256 | + JodaData data = new JodaData("John", jodaDateTime); |
| 257 | + |
| 258 | + assertThatThrownBy(() -> mapper.toJson(data)) |
| 259 | + .isInstanceOf(IOException.class); |
| 260 | + |
| 261 | + String json = "{\"name\":\"John\",\"jodaDate\":\"2000-01-01T00:00:00.000Z\"}"; |
| 262 | + assertThatThrownBy(() -> mapper.fromJson(json, JodaData.class)) |
| 263 | + .isInstanceOf(IOException.class); |
| 264 | + } |
| 265 | + |
| 266 | + private Set<String> getModuleNames(Collection<JacksonModule> modules) { |
| 267 | + return modules.stream() |
| 268 | + .map(JacksonModule::getModuleName) |
| 269 | + .collect(Collectors.toUnmodifiableSet()); |
| 270 | + } |
| 271 | + |
| 272 | + private static final class JodaModuleProvider { |
| 273 | + |
| 274 | + private static final JacksonModule MODULE = new JodaModule(); |
| 275 | + |
| 276 | + } |
| 277 | + |
| 278 | + private static final class KotlinModuleProvider { |
| 279 | + |
| 280 | + private static final JacksonModule MODULE = new KotlinModule.Builder().build(); |
| 281 | + |
| 282 | + } |
| 283 | + |
| 284 | + private List<JacksonModule> collectWellKnownModulesIfAvailable() { |
| 285 | + List<JacksonModule> modules = new ArrayList<>(); |
| 286 | + if (JODA_MODULE_PRESENT) { |
| 287 | + modules.add(JodaModuleProvider.MODULE); |
| 288 | + } |
| 289 | + if (KOTLIN_MODULE_PRESENT) { |
| 290 | + modules.add(KotlinModuleProvider.MODULE); |
| 291 | + } |
| 292 | + return modules; |
| 293 | + } |
| 294 | + |
| 295 | + private record TestData(String name, Optional<String> email, Optional<Integer> age) { |
| 296 | + } |
| 297 | + |
| 298 | + private record TimeData(LocalDateTime localDate, ZonedDateTime zoneDate) { |
| 299 | + } |
| 300 | + |
| 301 | + private record JodaData(String name, org.joda.time.DateTime jodaDate) { |
| 302 | + } |
| 303 | + |
| 304 | +} |
0 commit comments