jackson-module-kotlin

Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.

Github星跟蹤圖

Kotlin Build Status Kotlin Slack

Overview

Module that adds support for serialization/deserialization of Kotlin classes and data classes.
Previously a default constructor must have existed on the Kotlin object for Jackson to deserialize into the object.
With this module, single constructor classes can be used automatically, and those with secondary constructors or static factories are also supported.

Status

Build Status

2.9.8+ Releases are compiled with Kotlin 1.3.x, other older releases are Kotlin 1.2.x. All should be compatible with
current Kotlin if you also ensure the kotlin-reflect dependency is included with the same version number as stdlib.

  • release 2.10.10 (for Jackson 2.10.x)
  • release 2.9.10 (for Jackson 2.9.x)
  • release 2.8.11.1 (for Jackson 2.8.x)
  • release 2.7.9.1 (for Jackson 2.7.x) lacking in some new features from 2.8 branch

Releases require that you have included Kotlin stdlib and reflect libraries already.

Gradle:

compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.10.+"

Maven:

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-kotlin</artifactId>
    <version>2.10.10</version>
</dependency>

Usage

For any Kotlin class or data class constructor, the JSON property names will be inferred from the parameters using Kotlin runtime type information.

To use, just register the Kotlin module with your ObjectMapper instance:

val mapper = ObjectMapper().registerModule(KotlinModule())

or with the extension functions imported from import com.fasterxml.jackson.module.kotlin.*, one of:

val mapper = jacksonObjectMapper()
val mapper = ObjectMapper().registerKotlinModule()

A simple data class example:

import com.fasterxml.jackson.module.kotlin.*

data class MyStateObject(val name: String, val age: Int)

...
val mapper = jacksonObjectMapper()

val state = mapper.readValue<MyStateObject>(json)
// or
val state: MyStateObject = mapper.readValue(json)
// or
myMemberWithType = mapper.readValue(json)

All inferred types for the extension functions carry in full generic information (reified generics).
Therefore using readValue() extension without the Class parameter will reify the type and automatically create a TypeReference for Jackson.

Annotations

You can intermix non-field values in the constructor and JsonProperty annotation in the constructor.
Any fields not present in the constructor will be set after the constructor call.
An example of these concepts:

   @JsonInclude(JsonInclude.Include.NON_EMPTY)
   class StateObjectWithPartialFieldsInConstructor(val name: String, @JsonProperty("age") val years: Int)    {
        @JsonProperty("address") lateinit var primaryAddress: String   // set after construction
        var createdDt: DateTime by Delegates.notNull()                // set after construction
        var neverSetProperty: String? = null                          // not in JSON so must be nullable with default
    }

Note that using lateinit or Delegates.notNull() will ensure that the value is never null when read, while letting it be instantiated after the construction of the class.

Caveats

  • The @JsonCreator annotation is optional unless you have more than one constructor that is valid, or you want to use a static factory method (which also must have platformStatic annotation). In these cases, annotate only one method as JsonCreator.
  • Serializing a member or top-level Kotlin class that implements Iterator requires a workaround, see Issue #4 for easy workarounds.
  • If using proguard:
    • kotlin.Metadata annotations may be stripped, preventing deserialization. Add a proguard rule to keep the kotlin.Metadata class: -keep class kotlin.Metadata { *; }
    • If you're getting java.lang.ExceptionInInitializerError, you may also need: -keep class kotlin.reflect.** { *; }

Support for Kotlin Built-in classes

These Kotlin classes are supported with the following fields for serialization/deserialization (and other fields are hidden that are not relevant):

  • Pair (first, second)
  • Triple (first, second, third)
  • IntRange (start, end)
  • CharRange (start, end)
  • LongRange (start, end)

(others are likely to work, but may not be tuned for Jackson)

主要指標

概覽
名稱與所有者FasterXML/jackson-module-kotlin
主編程語言Kotlin
編程語言Kotlin (語言數: 3)
平台
許可證Apache License 2.0
所有者活动
創建於2014-08-27 20:20:03
推送於2025-05-31 14:06:08
最后一次提交
發布數153
最新版本名稱jackson-module-kotlin-3.0.0-rc5 (發布於 2025-05-22 10:17:16)
第一版名稱jackson-module-kotlin-2.4.3 (發布於 2014-10-15 10:08:47)
用户参与
星數1.1k
關注者數21
派生數179
提交數2.1k
已啟用問題?
問題數572
打開的問題數63
拉請求數349
打開的拉請求數0
關閉的拉請求數70
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?