CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-fasterxml-jackson-module--jackson-module-kotlin

Jackson module that adds comprehensive support for serialization and deserialization of Kotlin classes and data classes without requiring default constructors.

Pending
Overview
Eval results
Files

builtin-types.mddocs/

Built-in Type Support

Comprehensive serialization and deserialization support for Kotlin-specific types including unsigned numbers, sequences, and value classes.

Capabilities

Unsigned Number Types

Full serialization and deserialization support for Kotlin's unsigned integer types.

Unsigned Number Serializers

/**
 * Serializer for UByte values
 */
object UByteSerializer : StdSerializer<UByte>

/**
 * Serializer for UShort values  
 */
object UShortSerializer : StdSerializer<UShort>

/**
 * Serializer for UInt values
 */
object UIntSerializer : StdSerializer<UInt>

/**
 * Serializer for ULong values
 */
object ULongSerializer : StdSerializer<ULong>

Unsigned Number Deserializers

/**
 * Deserializer for UByte values
 */
object UByteDeserializer : StdDeserializer<UByte>

/**
 * Deserializer for UShort values
 */
object UShortDeserializer : StdDeserializer<UShort>

/**
 * Deserializer for UInt values
 */
object UIntDeserializer : StdDeserializer<UInt>

/**
 * Deserializer for ULong values
 */
object ULongDeserializer : StdDeserializer<ULong>

Unsigned Number Conversion Extensions

Safe conversion functions for unsigned number types.

/**
 * Convert Short to UByte safely
 * @return UByte value or null if out of range
 */
fun Short.asUByte(): UByte?

/**
 * Convert Int to UShort safely
 * @return UShort value or null if out of range
 */
fun Int.asUShort(): UShort?

/**
 * Convert Long to UInt safely
 * @return UInt value or null if out of range
 */
fun Long.asUInt(): UInt?

/**
 * Convert BigInteger to ULong safely
 * @return ULong value or null if out of range
 */
fun BigInteger.asULong(): ULong?

Usage Examples:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

data class UnsignedData(
    val smallNumber: UByte,
    val mediumNumber: UShort, 
    val largeNumber: UInt,
    val veryLargeNumber: ULong
)

val mapper = jacksonObjectMapper()

// Serialize unsigned numbers
val data = UnsignedData(
    smallNumber = 255u,
    mediumNumber = 65535u,
    largeNumber = 4294967295u,
    veryLargeNumber = 18446744073709551615u
)

val json = mapper.writeValueAsString(data)
println(json)
// Output: {"smallNumber":255,"mediumNumber":65535,"largeNumber":4294967295,"veryLargeNumber":18446744073709551615}

// Deserialize unsigned numbers
val deserialized = mapper.readValue<UnsignedData>(json)
println(deserialized)

// Safe conversion examples
val shortValue: Short = 200
val ubyteValue = shortValue.asUByte() // UByte(200)

val intValue: Int = 70000
val ushortValue = intValue.asUShort() // null (out of range)

Kotlin Collection Types

Built-in support for Kotlin-specific collection types.

Sequence Deserializer

/**
 * Deserializer for Kotlin Sequence type
 * Converts JSON arrays to lazy Kotlin sequences
 */
object SequenceDeserializer : StdDeserializer<Sequence<*>>

Usage Examples:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

data class SequenceData(val numbers: Sequence<Int>)

val mapper = jacksonObjectMapper()

// JSON with array
val json = """{"numbers":[1,2,3,4,5]}"""

// Deserialize to Sequence
val data = mapper.readValue<SequenceData>(json)

// Use sequence (lazy evaluation)
val evenNumbers = data.numbers
    .filter { it % 2 == 0 }
    .map { it * 2 }
    .toList()

println(evenNumbers) // [4, 8]

Regex Support

Built-in serialization and deserialization for Kotlin's Regex class.

Regex Deserializer

/**
 * Deserializer for Kotlin Regex type
 * Converts JSON strings to Regex instances
 */
object RegexDeserializer : StdDeserializer<Regex>

Usage Examples:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

data class ValidationRules(
    val emailPattern: Regex,
    val phonePattern: Regex
)

val mapper = jacksonObjectMapper()

// JSON with regex patterns
val json = """
{
    "emailPattern": "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}",
    "phonePattern": "\\d{3}-\\d{3}-\\d{4}"
}
""".trimIndent()

// Deserialize to Regex objects
val rules = mapper.readValue<ValidationRules>(json)

// Use regex patterns
val email = "test@example.com"
val isValidEmail = rules.emailPattern.matches(email)
println("Email valid: $isValidEmail") // true

val phone = "123-456-7890"
val isValidPhone = rules.phonePattern.matches(phone)
println("Phone valid: $isValidPhone") // true

Value Class Support

Support for Kotlin value classes (inline classes) with automatic boxing/unboxing.

Value Class Serializer

/**
 * Serializer for Kotlin value classes
 * Automatically unboxes value classes during serialization
 */
object ValueClassUnboxSerializer : StdSerializer<Any>

Usage Examples:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

// Define value classes
@JvmInline
value class UserId(val value: Long)

@JvmInline
value class Email(val value: String)

data class User(
    val id: UserId,
    val email: Email,
    val name: String
)

val mapper = jacksonObjectMapper()

// Serialize with value class unboxing
val user = User(
    id = UserId(12345L),
    email = Email("alice@example.com"),
    name = "Alice"
)

val json = mapper.writeValueAsString(user)
println(json)
// Output: {"id":12345,"email":"alice@example.com","name":"Alice"}

// Deserialize with value class boxing
val deserializedUser = mapper.readValue<User>(json)
println(deserializedUser.id.value) // 12345
println(deserializedUser.email.value) // alice@example.com

SimpleModule Extensions

Type-safe extensions for adding custom serializers and deserializers.

/**
 * Add serializer for Kotlin class
 * @param kClass Kotlin class to serialize
 * @param serializer JsonSerializer for the class
 * @return SimpleModule for method chaining
 */
fun <T : Any> SimpleModule.addSerializer(kClass: KClass<T>, serializer: JsonSerializer<T>): SimpleModule

/**
 * Add deserializer for Kotlin class
 * @param kClass Kotlin class to deserialize
 * @param deserializer JsonDeserializer for the class
 * @return SimpleModule for method chaining
 */
fun <T : Any> SimpleModule.addDeserializer(kClass: KClass<T>, deserializer: JsonDeserializer<T>): SimpleModule

Usage Examples:

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.addSerializer
import com.fasterxml.jackson.module.kotlin.addDeserializer
import kotlin.reflect.KClass

// Custom type
data class CustomId(val prefix: String, val number: Int) {
    override fun toString() = "$prefix-$number"
}

// Custom serializer
class CustomIdSerializer : JsonSerializer<CustomId>() {
    override fun serialize(value: CustomId, gen: JsonGenerator, serializers: SerializerProvider) {
        gen.writeString(value.toString())
    }
}

// Custom deserializer
class CustomIdDeserializer : JsonDeserializer<CustomId>() {
    override fun deserialize(p: JsonParser, ctxt: DeserializationContext): CustomId {
        val text = p.text
        val parts = text.split("-")
        return CustomId(parts[0], parts[1].toInt())
    }
}

// Register with module
val module = SimpleModule()
    .addSerializer(CustomId::class, CustomIdSerializer())
    .addDeserializer(CustomId::class, CustomIdDeserializer())

val mapper = jacksonObjectMapper().registerModule(module)

// Test custom serialization
val customId = CustomId("USER", 123)
val json = mapper.writeValueAsString(customId)
println(json) // "USER-123"

val deserialized = mapper.readValue<CustomId>(json)
println(deserialized) // CustomId(prefix=USER, number=123)

Kotlin Range Support

Built-in support for Kotlin range types.

Supported Range Types:

  • IntRange - serialized with start and end properties
  • LongRange - serialized with start and end properties
  • CharRange - serialized with start and end properties

Usage Examples:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

data class RangeData(
    val intRange: IntRange,
    val longRange: LongRange,
    val charRange: CharRange
)

val mapper = jacksonObjectMapper()

val data = RangeData(
    intRange = 1..10,
    longRange = 100L..200L,
    charRange = 'A'..'Z'
)

val json = mapper.writeValueAsString(data)
println(json)
// Output: {"intRange":{"start":1,"end":10},"longRange":{"start":100,"end":200},"charRange":{"start":"A","end":"Z"}}

val deserialized = mapper.readValue<RangeData>(json)
println(deserialized.intRange.toList()) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Built-in Kotlin Types

The module automatically supports these Kotlin built-in types:

  • Pair - serialized as {"first": value1, "second": value2}
  • Triple - serialized as {"first": value1, "second": value2, "third": value3}
  • kotlin.time.Duration - with JavaTimeModule integration when UseJavaDurationConversion is enabled

Usage Examples:

data class TupleData(
    val coordinates: Pair<Double, Double>,
    val rgb: Triple<Int, Int, Int>
)

val mapper = jacksonObjectMapper()

val data = TupleData(
    coordinates = 40.7128 to -74.0060,  // New York coordinates
    rgb = Triple(255, 128, 0)  // Orange color
)

val json = mapper.writeValueAsString(data)
val deserialized = mapper.readValue<TupleData>(json)

Install with Tessl CLI

npx tessl i tessl/maven-com-fasterxml-jackson-module--jackson-module-kotlin

docs

builtin-types.md

factory-functions.md

index.md

json-node-operations.md

module-configuration.md

type-safe-extensions.md

tile.json