Jackson module that adds comprehensive support for serialization and deserialization of Kotlin classes and data classes without requiring default constructors.
—
Reified extension functions that eliminate the need for explicit type references and provide compile-time type safety for all ObjectMapper operations.
Create TypeReference instances using reified generics for type-safe Jackson operations.
/**
* Create a TypeReference for reified type T
* @return TypeReference<T> for use with Jackson operations
*/
inline fun <reified T> jacksonTypeRef(): TypeReference<T>Usage Examples:
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
// Create type references for complex types
val listTypeRef = jacksonTypeRef<List<String>>()
val mapTypeRef = jacksonTypeRef<Map<String, User>>()
// Use with ObjectMapper directly
val users: List<User> = mapper.readValue(json, jacksonTypeRef<List<User>>())Type-safe readValue operations with automatic type inference from various input sources.
/**
* Read value from JsonParser with type inference
* @param jp JsonParser to read from
* @return Deserialized object of type T
* @throws RuntimeJsonMappingException if T is non-null and value is null
*/
inline fun <reified T> ObjectMapper.readValue(jp: JsonParser): T
/**
* Read value from File with type inference
* @param src File to read from
* @return Deserialized object of type T
*/
inline fun <reified T> ObjectMapper.readValue(src: File): T
/**
* Read value from URL with type inference
* @param src URL to read from
* @return Deserialized object of type T
*/
inline fun <reified T> ObjectMapper.readValue(src: URL): T
/**
* Read value from String with type inference
* @param content JSON string to parse
* @return Deserialized object of type T
*/
inline fun <reified T> ObjectMapper.readValue(content: String): T
/**
* Read value from Reader with type inference
* @param src Reader to read from
* @return Deserialized object of type T
*/
inline fun <reified T> ObjectMapper.readValue(src: Reader): T
/**
* Read value from InputStream with type inference
* @param src InputStream to read from
* @return Deserialized object of type T
*/
inline fun <reified T> ObjectMapper.readValue(src: InputStream): T
/**
* Read value from ByteArray with type inference
* @param src ByteArray to parse
* @return Deserialized object of type T
*/
inline fun <reified T> ObjectMapper.readValue(src: ByteArray): TUsage Examples:
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.io.File
data class User(val name: String, val age: Int)
val mapper = jacksonObjectMapper()
// Read from different sources with type inference
val user1 = mapper.readValue<User>("""{"name":"Alice","age":30}""")
val user2: User = mapper.readValue("""{"name":"Bob","age":25}""")
val user3 = mapper.readValue<User>(File("user.json"))
val user4 = mapper.readValue<User>(userInputStream)
// Complex types
val users = mapper.readValue<List<User>>(usersJson)
val userMap = mapper.readValue<Map<String, User>>(userMapJson)Type-safe readValues operation for reading multiple objects from a single input.
/**
* Read multiple values from JsonParser with type inference
* @param jp JsonParser to read from
* @return MappingIterator<T> for iterating over values
* @throws RuntimeJsonMappingException if T is non-null and any value is null
*/
inline fun <reified T> ObjectMapper.readValues(jp: JsonParser): MappingIterator<T>Usage Examples:
import com.fasterxml.jackson.module.kotlin.readValues
// Read multiple JSON objects from a stream
val parser = mapper.factory.createParser(jsonStream)
val userIterator = mapper.readValues<User>(parser)
for (user in userIterator) {
println("User: ${user.name}, Age: ${user.age}")
}Type-safe tree-to-value and object conversion operations.
/**
* Convert TreeNode to typed value
* @param n TreeNode to convert
* @return Converted object of type T
*/
inline fun <reified T> ObjectMapper.treeToValue(n: TreeNode): T
/**
* Convert any object to typed value
* @param from Source object to convert
* @return Converted object of type T
*/
inline fun <reified T> ObjectMapper.convertValue(from: Any?): TUsage Examples:
import com.fasterxml.jackson.module.kotlin.convertValue
import com.fasterxml.jackson.module.kotlin.treeToValue
// Convert between different object types
val userMap = mapOf("name" to "Charlie", "age" to 35)
val user = mapper.convertValue<User>(userMap)
// Convert JSON tree to typed object
val jsonNode = mapper.readTree(json)
val user2 = mapper.treeToValue<User>(jsonNode)
// Convert with complex types
val userList = mapper.convertValue<List<User>>(listOfMaps)Type-safe configuration methods using reified generics.
/**
* Get configuration override for reified type
* @return MutableConfigOverride for type T
*/
inline fun <reified T : Any> ObjectMapper.configOverride(): MutableConfigOverride
/**
* Add mix-in annotation for reified types
* @return ObjectMapper for method chaining
*/
inline fun <reified T, reified U> ObjectMapper.addMixIn(): ObjectMapperUsage Examples:
import com.fasterxml.jackson.module.kotlin.addMixIn
import com.fasterxml.jackson.module.kotlin.configOverride
// Configure specific type behavior
mapper.configOverride<User>()
.setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, null))
// Add mix-in annotations
mapper.addMixIn<User, UserMixin>()
interface UserMixin {
@get:JsonProperty("full_name")
val name: String
}Type-safe extensions for ObjectReader operations.
/**
* Type-safe ObjectReader.readValue
* @param jp JsonParser to read from
* @return Deserialized object of type T
*/
inline fun <reified T> ObjectReader.readValueTyped(jp: JsonParser): T
/**
* Type-safe ObjectReader.readValues
* @param jp JsonParser to read from
* @return Iterator<T> for reading multiple values
*/
inline fun <reified T> ObjectReader.readValuesTyped(jp: JsonParser): Iterator<T>
/**
* Convert TreeNode to typed value using ObjectReader
* @param n TreeNode to convert
* @return Converted object of type T or null
*/
inline fun <reified T> ObjectReader.treeToValue(n: TreeNode): T?Usage Examples:
import com.fasterxml.jackson.module.kotlin.readValueTyped
import com.fasterxml.jackson.module.kotlin.readValuesTyped
// Create reader for specific type
val userReader = mapper.readerFor(User::class.java)
// Use type-safe reader operations
val parser = mapper.factory.createParser(json)
val user = userReader.readValueTyped<User>(parser)
// Read multiple values
val usersIterator = userReader.readValuesTyped<User>(parser)Type-safe extensions for JsonMapper.Builder configuration.
/**
* Add mix-in annotation for JsonMapper builder
* @return JsonMapper.Builder for method chaining
*/
inline fun <reified T, reified U> JsonMapper.Builder.addMixIn(): JsonMapper.BuilderUsage Examples:
import com.fasterxml.jackson.module.kotlin.addMixIn
import com.fasterxml.jackson.module.kotlin.jsonMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
val mapper = jsonMapper {
addModule(kotlinModule())
addMixIn<User, UserMixin>()
}Type-safe configuration within module setup contexts.
/**
* Get configuration override in setup context
* @return MutableConfigOverride for type T
*/
inline fun <reified T : Any> Module.SetupContext.configOverride(): MutableConfigOverrideUsage Examples:
import com.fasterxml.jackson.module.kotlin.configOverride
class CustomModule : SimpleModule() {
override fun setupModule(context: SetupContext) {
super.setupModule(context)
// Configure specific types during module setup
context.configOverride<User>()
.setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, null))
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-fasterxml-jackson-module--jackson-module-kotlin