0
# Factory Functions
1
2
Convenient factory functions for creating Jackson ObjectMappers and JsonMappers with Kotlin support enabled by default.
3
4
## Capabilities
5
6
### Jackson ObjectMapper Factory
7
8
Create ObjectMapper instances with Kotlin module pre-registered for immediate use with Kotlin classes.
9
10
```kotlin { .api }
11
/**
12
* Create an ObjectMapper with Kotlin module registered
13
* @return ObjectMapper configured for Kotlin support
14
*/
15
fun jacksonObjectMapper(): ObjectMapper
16
17
/**
18
* Create an ObjectMapper with custom Kotlin module configuration
19
* @param initializer Lambda for configuring KotlinModule.Builder
20
* @return ObjectMapper configured with custom Kotlin settings
21
*/
22
fun jacksonObjectMapper(initializer: KotlinModule.Builder.() -> Unit): ObjectMapper
23
```
24
25
**Usage Examples:**
26
27
```kotlin
28
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
29
import com.fasterxml.jackson.module.kotlin.KotlinFeature
30
31
// Simple mapper with default Kotlin support
32
val mapper = jacksonObjectMapper()
33
34
// Mapper with custom Kotlin configuration
35
val customMapper = jacksonObjectMapper {
36
enable(KotlinFeature.NullToEmptyCollection)
37
enable(KotlinFeature.SingletonSupport)
38
withReflectionCacheSize(1024)
39
}
40
41
data class User(val name: String, val age: Int)
42
val user = User("Alice", 30)
43
val json = mapper.writeValueAsString(user)
44
```
45
46
### JsonMapper Builder Factory
47
48
Create JsonMapper.Builder instances with Kotlin module pre-added for fluent configuration.
49
50
```kotlin { .api }
51
/**
52
* Create a JsonMapper.Builder with Kotlin module added
53
* @return JsonMapper.Builder configured for Kotlin support
54
*/
55
fun jacksonMapperBuilder(): JsonMapper.Builder
56
57
/**
58
* Create a JsonMapper.Builder with custom Kotlin module configuration
59
* @param initializer Lambda for configuring KotlinModule.Builder
60
* @return JsonMapper.Builder configured with custom Kotlin settings
61
*/
62
fun jacksonMapperBuilder(initializer: KotlinModule.Builder.() -> Unit): JsonMapper.Builder
63
```
64
65
**Usage Examples:**
66
67
```kotlin
68
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
69
import com.fasterxml.jackson.module.kotlin.KotlinFeature
70
71
// Simple builder with default Kotlin support
72
val mapper = jacksonMapperBuilder().build()
73
74
// Builder with custom Kotlin configuration and other Jackson features
75
val customMapper = jacksonMapperBuilder {
76
enable(KotlinFeature.StrictNullChecks)
77
enable(KotlinFeature.NullIsSameAsDefault)
78
}
79
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
80
.build()
81
```
82
83
### JsonMapper Factory
84
85
Create JsonMapper instances directly with optional configuration.
86
87
```kotlin { .api }
88
/**
89
* Create a JsonMapper with optional configuration
90
* @param initializer Lambda for configuring JsonMapper.Builder
91
* @return JsonMapper instance
92
*/
93
fun jsonMapper(initializer: JsonMapper.Builder.() -> Unit = {}): JsonMapper
94
```
95
96
**Usage Examples:**
97
98
```kotlin
99
import com.fasterxml.jackson.module.kotlin.jsonMapper
100
import com.fasterxml.jackson.module.kotlin.kotlinModule
101
import com.fasterxml.jackson.module.kotlin.KotlinFeature
102
103
// Simple JsonMapper
104
val mapper = jsonMapper {
105
addModule(kotlinModule())
106
}
107
108
// JsonMapper with custom Kotlin and Jackson configuration
109
val advancedMapper = jsonMapper {
110
addModule(kotlinModule {
111
enable(KotlinFeature.SingletonSupport)
112
enable(KotlinFeature.UseJavaDurationConversion)
113
})
114
configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true)
115
}
116
```
117
118
### KotlinModule Factory
119
120
Create KotlinModule instances with optional configuration for manual registration.
121
122
```kotlin { .api }
123
/**
124
* Create a KotlinModule with optional configuration
125
* @param initializer Lambda for configuring KotlinModule.Builder
126
* @return KotlinModule instance
127
*/
128
fun kotlinModule(initializer: KotlinModule.Builder.() -> Unit = {}): KotlinModule
129
```
130
131
**Usage Examples:**
132
133
```kotlin
134
import com.fasterxml.jackson.module.kotlin.kotlinModule
135
import com.fasterxml.jackson.module.kotlin.KotlinFeature
136
import com.fasterxml.jackson.databind.ObjectMapper
137
138
// Create module for manual registration
139
val module = kotlinModule {
140
enable(KotlinFeature.NullToEmptyCollection)
141
enable(KotlinFeature.NullToEmptyMap)
142
withReflectionCacheSize(2048)
143
}
144
145
val mapper = ObjectMapper().registerModule(module)
146
```
147
148
### Registration Extensions
149
150
Extension functions for adding Kotlin support to existing ObjectMapper instances.
151
152
```kotlin { .api }
153
/**
154
* Register KotlinModule with an existing ObjectMapper
155
* @return The same ObjectMapper instance for method chaining
156
*/
157
fun ObjectMapper.registerKotlinModule(): ObjectMapper
158
159
/**
160
* Register KotlinModule with custom configuration to an existing ObjectMapper
161
* @param initializer Lambda for configuring KotlinModule.Builder
162
* @return The same ObjectMapper instance for method chaining
163
*/
164
fun ObjectMapper.registerKotlinModule(initializer: KotlinModule.Builder.() -> Unit): ObjectMapper
165
```
166
167
**Usage Examples:**
168
169
```kotlin
170
import com.fasterxml.jackson.databind.ObjectMapper
171
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
172
import com.fasterxml.jackson.module.kotlin.KotlinFeature
173
174
// Add Kotlin support to existing mapper
175
val existingMapper = ObjectMapper()
176
existingMapper.registerKotlinModule()
177
178
// Add Kotlin support with custom configuration
179
val customMapper = ObjectMapper()
180
.registerKotlinModule {
181
enable(KotlinFeature.SingletonSupport)
182
enable(KotlinFeature.NewStrictNullChecks)
183
}
184
.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true)
185
```
186
187
## Integration Patterns
188
189
### Spring Boot Integration
190
191
```kotlin
192
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
193
import com.fasterxml.jackson.module.kotlin.KotlinFeature
194
import org.springframework.context.annotation.Bean
195
import org.springframework.context.annotation.Configuration
196
197
@Configuration
198
class JacksonConfig {
199
200
@Bean
201
fun objectMapper() = jacksonObjectMapper {
202
enable(KotlinFeature.NullToEmptyCollection)
203
enable(KotlinFeature.SingletonSupport)
204
}
205
}
206
```
207
208
### Ktor Integration
209
210
```kotlin
211
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
212
import io.ktor.serialization.jackson.*
213
import io.ktor.server.application.*
214
import io.ktor.server.plugins.contentnegotiation.*
215
216
fun Application.configureSerialization() {
217
install(ContentNegotiation) {
218
jackson {
219
registerKotlinModule()
220
}
221
}
222
}
223
```