Skip to content

Commit

Permalink
Merge pull request #741 from k163377/register-kotlin-module
Browse files Browse the repository at this point in the history
Changed to allow KotlinFeature to be set in the function that registers a KotlinModule
  • Loading branch information
k163377 committed Dec 10, 2023
2 parents 6883c16 + 20a5d5c commit c53e606
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
24 changes: 2 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,8 @@ val mapper = jsonMapper {
}
```

<details>
<summary>Jackson versions prior to 2.10–2.11</summary>

```kotlin
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
...
val mapper = JsonMapper.builder().addModule(KotlinModule()).build()
```
</details>


<details>
<summary>Jackson versions prior to 2.10</summary>

```kotlin
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
...
val mapper = ObjectMapper().registerModule(KotlinModule())
```
</details>
In 2.17 and later, the `jacksonObjectMapper {}` and `registerKotlinModule {}` lambdas allow configuration for `KotlinModule`.
See [#Configuration](#Configuration) for details on the available configuration items.

A simple data class example:
```kotlin
Expand Down
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Contributors:
# 2.17.0 (not yet released)

WrongWrong (@k163377)
* #741: Changed to allow KotlinFeature to be set in the function that registers a KotlinModule.
* #740: Reduce conversion cache from Executable to KFunction.
* #738: Fix JacksonInject priority.
* #732: SequenceSerializer removed.
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Co-maintainers:

2.17.0 (not yet released)

#741: Changed to allow KotlinFeature to be set in the function that registers a KotlinModule.
The `jacksonObjectMapper {}` and `registerKotlinModule {}` lambdas allow configuration for KotlinModule.
#740: Reduce conversion cache from Executable to KFunction.
This will reduce memory usage efficiency and total memory consumption, but may result in a minor performance degradation in use cases where a large number of factory functions are used as JsonCreator.
#738: JacksonInject is now preferred over the default argument(fixes #722).
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ fun jsonMapper(initializer: JsonMapper.Builder.() -> Unit = {}): JsonMapper {
return builder.build()
}

fun jacksonObjectMapper(): ObjectMapper = jsonMapper { addModule(kotlinModule()) }
fun jacksonMapperBuilder(): JsonMapper.Builder = JsonMapper.builder().addModule(kotlinModule())

fun ObjectMapper.registerKotlinModule(): ObjectMapper = this.registerModule(kotlinModule())
// region: JvmOverloads is set for bytecode compatibility for versions below 2.17.
@JvmOverloads
fun jacksonObjectMapper(initializer: KotlinModule.Builder.() -> Unit = {}): ObjectMapper =
jsonMapper { addModule(kotlinModule(initializer)) }
@JvmOverloads
fun jacksonMapperBuilder(initializer: KotlinModule.Builder.() -> Unit = {}): JsonMapper.Builder =
JsonMapper.builder().addModule(kotlinModule(initializer))

@JvmOverloads
fun ObjectMapper.registerKotlinModule(initializer: KotlinModule.Builder.() -> Unit = {}): ObjectMapper =
this.registerModule(kotlinModule(initializer))
// endregion

inline fun <reified T> jacksonTypeRef(): TypeReference<T> = object: TypeReference<T>() {}

Expand Down

0 comments on commit c53e606

Please sign in to comment.