Skip to content

Commit 1f9a9cf

Browse files
committed
Auto-detect Kotlin Jackson module
Issue: SPR-14108
1 parent edea66a commit 1f9a9cf

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ project("spring-web") {
739739
}
740740
testCompile("com.fasterxml.jackson.datatype:jackson-datatype-joda:${jackson2Version}")
741741
testCompile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${jackson2Version}")
742+
testCompile("com.fasterxml.jackson.module:jackson-module-kotlin:${jackson2Version}")
742743
testRuntime("com.sun.mail:javax.mail:${javamailVersion}")
743744
}
744745
}

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* <li><a href="https://github.com/FasterXML/jackson-datatype-jdk8">jackson-datatype-jdk8</a>: support for other Java 8 types like {@link java.util.Optional}</li>
7676
* <li><a href="https://github.com/FasterXML/jackson-datatype-jsr310">jackson-datatype-jsr310</a>: support for Java 8 Date & Time API types</li>
7777
* <li><a href="https://github.com/FasterXML/jackson-datatype-joda">jackson-datatype-joda</a>: support for Joda-Time types</li>
78+
* <li><a href="https://github.com/FasterXML/jackson-module-kotlin">jackson-module-kotlin</a>: support for Kotlin classes and data classes</li>
7879
* </ul>
7980
*
8081
* <p>Compatible with Jackson 2.6 and higher, as of Spring 4.3.
@@ -747,6 +748,18 @@ private void registerWellKnownModulesIfAvailable(ObjectMapper objectMapper) {
747748
// jackson-datatype-joda not available
748749
}
749750
}
751+
752+
// Kotlin present?
753+
if (ClassUtils.isPresent("kotlin.Unit", this.moduleClassLoader)) {
754+
try {
755+
Class<? extends Module> kotlinModule = (Class<? extends Module>)
756+
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", this.moduleClassLoader);
757+
objectMapper.registerModule(BeanUtils.instantiate(kotlinModule));
758+
}
759+
catch (ClassNotFoundException ex) {
760+
// jackson-module-kotlin not available
761+
}
762+
}
750763
}
751764

752765

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
* <li><a href="https://github.com/FasterXML/jackson-datatype-jdk8">jackson-datatype-jdk8</a>: support for other Java 8 types like {@link java.util.Optional}</li>
117117
* <li><a href="https://github.com/FasterXML/jackson-datatype-jsr310">jackson-datatype-jsr310</a>: support for Java 8 Date & Time API types</li>
118118
* <li><a href="https://github.com/FasterXML/jackson-datatype-joda">jackson-datatype-joda</a>: support for Joda-Time types</li>
119+
* <li><a href="https://github.com/FasterXML/jackson-module-kotlin">jackson-module-kotlin</a>: support for Kotlin classes and data classes</li>
119120
* </ul>
120121
*
121122
* <p>In case you want to configure Jackson's {@link ObjectMapper} with a custom {@link Module},

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.fasterxml.jackson.databind.type.SimpleType;
6666
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
6767

68+
import kotlin.ranges.IntRange;
6869
import org.joda.time.DateTime;
6970
import org.joda.time.DateTimeZone;
7071
import org.junit.Test;
@@ -255,6 +256,10 @@ public void wellKnownModules() throws JsonProcessingException, UnsupportedEncodi
255256

256257
Optional<String> optional = Optional.of("test");
257258
assertEquals("\"test\"", new String(objectMapper.writeValueAsBytes(optional), "UTF-8"));
259+
260+
// Kotlin module
261+
IntRange range = new IntRange(1, 3);
262+
assertEquals("{\"start\":1,\"end\":3}", new String(objectMapper.writeValueAsBytes(range), "UTF-8"));
258263
}
259264

260265
@Test // SPR-12634
@@ -328,8 +333,8 @@ public void mixIn() {
328333
Class<?> target = String.class;
329334
Class<?> mixInSource = Object.class;
330335

331-
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().mixIn(target,
332-
mixInSource).build();
336+
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules()
337+
.mixIn(target, mixInSource).build();
333338

334339
assertEquals(1, objectMapper.mixInCount());
335340
assertSame(mixInSource, objectMapper.findMixInClassFor(target));
@@ -342,7 +347,8 @@ public void mixIns() {
342347
Map<Class<?>, Class<?>> mixIns = new HashMap<Class<?>, Class<?>>();
343348
mixIns.put(target, mixInSource);
344349

345-
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().mixIns(mixIns).build();
350+
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules()
351+
.mixIns(mixIns).build();
346352

347353
assertEquals(1, objectMapper.mixInCount());
348354
assertSame(mixInSource, objectMapper.findMixInClassFor(target));

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public void setMixIns() {
285285
Map<Class<?>, Class<?>> mixIns = new HashMap<Class<?>, Class<?>>();
286286
mixIns.put(target, mixinSource);
287287

288+
this.factory.setModules(Collections.emptyList());
288289
this.factory.setMixIns(mixIns);
289290
this.factory.afterPropertiesSet();
290291
ObjectMapper objectMapper = this.factory.getObject();

0 commit comments

Comments
 (0)