Auto-detect Kotlin Jackson module
Issue: SPR-14108
This commit is contained in:
@@ -739,6 +739,7 @@ project("spring-web") {
|
||||
}
|
||||
testCompile("com.fasterxml.jackson.datatype:jackson-datatype-joda:${jackson2Version}")
|
||||
testCompile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${jackson2Version}")
|
||||
testCompile("com.fasterxml.jackson.module:jackson-module-kotlin:${jackson2Version}")
|
||||
testRuntime("com.sun.mail:javax.mail:${javamailVersion}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ import org.springframework.util.StringUtils;
|
||||
* <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>
|
||||
* <li><a href="https://github.com/FasterXML/jackson-datatype-jsr310">jackson-datatype-jsr310</a>: support for Java 8 Date & Time API types</li>
|
||||
* <li><a href="https://github.com/FasterXML/jackson-datatype-joda">jackson-datatype-joda</a>: support for Joda-Time types</li>
|
||||
* <li><a href="https://github.com/FasterXML/jackson-module-kotlin">jackson-module-kotlin</a>: support for Kotlin classes and data classes</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Compatible with Jackson 2.6 and higher, as of Spring 4.3.
|
||||
@@ -747,6 +748,18 @@ public class Jackson2ObjectMapperBuilder {
|
||||
// jackson-datatype-joda not available
|
||||
}
|
||||
}
|
||||
|
||||
// Kotlin present?
|
||||
if (ClassUtils.isPresent("kotlin.Unit", this.moduleClassLoader)) {
|
||||
try {
|
||||
Class<? extends Module> kotlinModule = (Class<? extends Module>)
|
||||
ClassUtils.forName("com.fasterxml.jackson.module.kotlin.KotlinModule", this.moduleClassLoader);
|
||||
objectMapper.registerModule(BeanUtils.instantiate(kotlinModule));
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// jackson-module-kotlin not available
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -116,6 +116,7 @@ import org.springframework.context.ApplicationContextAware;
|
||||
* <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>
|
||||
* <li><a href="https://github.com/FasterXML/jackson-datatype-jsr310">jackson-datatype-jsr310</a>: support for Java 8 Date & Time API types</li>
|
||||
* <li><a href="https://github.com/FasterXML/jackson-datatype-joda">jackson-datatype-joda</a>: support for Joda-Time types</li>
|
||||
* <li><a href="https://github.com/FasterXML/jackson-module-kotlin">jackson-module-kotlin</a>: support for Kotlin classes and data classes</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>In case you want to configure Jackson's {@link ObjectMapper} with a custom {@link Module},
|
||||
|
||||
@@ -65,6 +65,7 @@ import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
|
||||
import com.fasterxml.jackson.databind.type.SimpleType;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import kotlin.ranges.IntRange;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.Test;
|
||||
@@ -255,6 +256,10 @@ public class Jackson2ObjectMapperBuilderTests {
|
||||
|
||||
Optional<String> optional = Optional.of("test");
|
||||
assertEquals("\"test\"", new String(objectMapper.writeValueAsBytes(optional), "UTF-8"));
|
||||
|
||||
// Kotlin module
|
||||
IntRange range = new IntRange(1, 3);
|
||||
assertEquals("{\"start\":1,\"end\":3}", new String(objectMapper.writeValueAsBytes(range), "UTF-8"));
|
||||
}
|
||||
|
||||
@Test // SPR-12634
|
||||
@@ -328,8 +333,8 @@ public class Jackson2ObjectMapperBuilderTests {
|
||||
Class<?> target = String.class;
|
||||
Class<?> mixInSource = Object.class;
|
||||
|
||||
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().mixIn(target,
|
||||
mixInSource).build();
|
||||
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules()
|
||||
.mixIn(target, mixInSource).build();
|
||||
|
||||
assertEquals(1, objectMapper.mixInCount());
|
||||
assertSame(mixInSource, objectMapper.findMixInClassFor(target));
|
||||
@@ -342,7 +347,8 @@ public class Jackson2ObjectMapperBuilderTests {
|
||||
Map<Class<?>, Class<?>> mixIns = new HashMap<Class<?>, Class<?>>();
|
||||
mixIns.put(target, mixInSource);
|
||||
|
||||
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().mixIns(mixIns).build();
|
||||
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules()
|
||||
.mixIns(mixIns).build();
|
||||
|
||||
assertEquals(1, objectMapper.mixInCount());
|
||||
assertSame(mixInSource, objectMapper.findMixInClassFor(target));
|
||||
|
||||
@@ -285,6 +285,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
Map<Class<?>, Class<?>> mixIns = new HashMap<Class<?>, Class<?>>();
|
||||
mixIns.put(target, mixinSource);
|
||||
|
||||
this.factory.setModules(Collections.emptyList());
|
||||
this.factory.setMixIns(mixIns);
|
||||
this.factory.afterPropertiesSet();
|
||||
ObjectMapper objectMapper = this.factory.getObject();
|
||||
|
||||
Reference in New Issue
Block a user