Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.DanielThomas:jackson-module-kotlin:2.5.5-2b'
}
<dependency>
<groupId>com.github.DanielThomas</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.5.5-2b</version>
</dependency>
libraryDependencies += "com.github.DanielThomas" % "jackson-module-kotlin" % "2.5.5-2b"
:dependencies [[com.github.DanielThomas/jackson-module-kotlin "2.5.5-2b"]]
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.
Releases are available on Maven Central:
future releases 2.9.x
are for Kotlin 1.1.+
only. All previous releases will also work for 1.1.+
if you include kotlin-reflect
dependency at same version level.
release 2.8.9
(for Jackson 2.8.x
) now supports using default values in constructor and creator methods
2.7.9
(for Jackson 2.7.x
) lacking in some new features from 2.8 branch2.6.7
(for Jackson 2.6.x
) lacking in some new features from 2.8 branch2.5.5-2
(for Jackson 2.5.x
) lacking in some new features from 2.8 branchReleases require that you have included Kotlin stdlib and reflect libraries already.
Special note for Kotlin 1.1.0, you must include kotlin-reflect
dependency in your project directly with a matching Kotlin version or you will encounter random errors.
Gradle:
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.0"
Maven:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.9.0</version>
</dependency>
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.
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.
@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
.These Kotlin classes are supported with the following fields for serialization/deserialization (and other fields are hidden that are not relevant):
(others are likely to work, but may not be tuned for Jackson)