Default params is a Kotlin feature. Jackson does not know about it hence we need to inform it somehow to be able to support it. Thankfully there's the excellent jackson-module-kotlin which let's you do:

val mapper: ObjectMapper = ObjectMapper()
        .registerKotlinModule() //let Jackson know about Kotlin via extension method
...
data class SomeData(
    private val notInJson: String = "some default value",
    private val inJson: String
)
...
val data = mapper.readValue<SomeData>("""{"inJson": "sample value"}""")
println(data) // -> SomeData(notInJson=some default value, inJson=sample value)

For instructions how to use it take a look at jackson-module-kotlin Readme and pay special attention to versions of the module and Jackson itself. In order for the default parameters to work you need at least version 2.8.4 of jackson-module-kotlin

Answer from miensol on Stack Overflow
🌐
Reddit
reddit.com › r/kotlin › [question] jackson default data class constructor problem with proguard
r/Kotlin on Reddit: [Question] Jackson default data class constructor problem with proguard
August 9, 2016 -

Hi I am having a problem with a kotlin data class and proguard.

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.app.models.tile.TileConfig]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)

found for type". I have this on my config file for keeping jackson and the package I am having problem with

-keep  class com.app.models.** 
-keep class com.fasterxml.jackson.**

I also use the jackson kotlin plugin. This is my data class

data class TileConfig( var color: String?, var icons: List<TileConfigIcon > = ArrayList())

does anyone know how to fix this problem?

Discussions

Deserialization fails for a data class where one or more constructor arguments have no default
If the JSON structure contains ... the constructor that is available. If the JSON structure does not contain a property called sendEmail, deserialisation can't create an instance (as sendEmail is not nullable) and throws a JSON parse error. Whilst the error I'm seeing is raised by a Spring class (MappingJackson2HttpMessageConverter) I'm assuming that the root cause is a corner case in a Jackson module being leveraged to do the Kotlin-specific heavy ... More on github.com
🌐 github.com
6
July 24, 2018
See if there is benefit from integrating with jackson-databind better wrt detecting "default" Constructor for Kotlin (data) classes
See if there is benefit from integrating with jackson-databind better wrt detecting "default" Constructor for Kotlin (data) classes#805 More on github.com
🌐 github.com
8
June 11, 2024
json - How do you make Jackson use Kotlin default params for missing values? - Stack Overflow
I've a Kotlin data class that looks like this, data class SomeData( private val notInJson: String = "some default value", private val inJson: String ) and a json string, I wish to deserializ... More on stackoverflow.com
🌐 stackoverflow.com
Unexpected behavior when using `NON_DEFAULT` with `data class`
This class data class has default ... constructor (at least from the perspective of the Kotlin language). However, this class is serialized as if the default value for flag is false rather than true. This causes the below roundtrip to fail. ... import com.fasterxml.jackson.annotatio... More on github.com
🌐 github.com
July 19, 2021
🌐
Medium
proandroiddev.com › parsing-optional-values-with-jackson-and-kotlin-36f6f63868ef
Parsing data with Jackson using kotlin | ProAndroidDev
May 7, 2020 - Data classes has a restriction to have at-least one primary constructor parameter so no default/no-args constructor is available by default in data classes. Jackson is unable to find the default constructor so it’s unable to create an object ...
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin
GitHub - FasterXML/jackson-module-kotlin: Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes. · GitHub
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.
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 170
Deserialization fails for a data class where one or more constructor arguments have no default · Issue #170 · FasterXML/jackson-module-kotlin
July 24, 2018 - Through experimentation I've ... Boolean = false) This would appear to me to be a bug - default values are optional in a Kotlin data class constructor....
Author: FasterXML
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 805
See if there is benefit from integrating with jackson-databind better wrt detecting "default" Constructor for Kotlin (data) classes · Issue #805 · FasterXML/jackson-module-kotlin
June 11, 2024 - With 2.18 jackson-databind has a new, rewritten logic for POJO Property detection: work was done mostly under FasterXML/jackson-databind#4515. Logic, at high-level, selects at most one "Properties-based" Creator (constructor / static factory method), in following order: Explicitly annotated (either @JsonCreator on ctor/factory, or at least one parameter of ctor/factory annotated with @JsonProperty) Default/preferred/primary -- currently only detected for Record types (canonical constructor)
Author: FasterXML
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 478
Unexpected behavior when using `NON_DEFAULT` with `data class` · Issue #478 · FasterXML/jackson-module-kotlin
July 19, 2021 - This class data class has default ... constructor (at least from the perspective of the Kotlin language). However, this class is serialized as if the default value for flag is false rather than true. This causes the below roundtrip to fail. ... import com.fasterxml.jackson.annotatio...
Find elsewhere
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 16665966 › i-have-this-simple-data-class-what-should-i-do-to-make-it-po
I have this simple data class What should I do to make it po kotlinlang #getting-started
March 14, 2024 - data class MyClass @JsonCreator constructor ( var changedAt: Date, var from: Int, var to: Int, var percentChanged: Int, var levelUsed: Int, ) ... If you have, I would expect data classes to work out of the box.
🌐
Baeldung
baeldung.com › home › libraries › jackson support for kotlin
Jackson Support for Kotlin | Baeldung on Kotlin
August 26, 2026 - The latest version of jackson-module-kotlin can be found on Maven Central. Let’s start with object serialization. Here we have a simple data Movie class that we’ll use in our examples:
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 591
Can I deserialize a data class with an unsigned member in the constructor? · Issue #591 · FasterXML/jackson-module-kotlin
October 21, 2022 - Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of A (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
Author: FasterXML
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 482354 › i-want-to-use-kotlin-data-class-in-java-with-jackson-seriali
I want to use Kotlin data class in java with jackson seriali kotlinlang #jackson-kotlin
I want to use Kotlin data class in java with jackson serializer. If I want to use koltin data class from Java then I must set default parameters to create a default constructor for jackson serializer.
Top answer
1 of 9
102

@JsonProperty annotations in your code are all put on private fields within your data class and by default Jackson doesn't scan private fields for annotations. You have to instruct it to do otherwise by putting @JsonAutoDetect annotation:

@JsonAutoDetect(fieldVisibility = Visibility.ANY)
data class CurrencyInfo(
    @JsonProperty("currency_info") var currencyInfo: CurrencyInfoItem?
)

or alternatively you can move your annotations on accessor methods:

data class CurrencyInfo(
    @get:JsonProperty("currency_info") var currencyInfo: CurrencyInfoItem?
)
2 of 9
22

You can do something like this:

data class CurrencyInfo @JsonCreator constructor (
        @param:JsonProperty("currency_info") 
        @get:JsonProperty("currency_info")
        val currencyInfo: CurrencyInfoItem?
)

code above translates to java as:

public final class CurrencyInfo {
   @Nullable
   private final String currencyInfo;

   @JsonProperty("currency_info")
   @Nullable
   public final String getCurrencyInfo() {
      return this.currencyInfo;
   }

   @JsonCreator
   public CurrencyInfo(@JsonProperty("currency_info") @Nullable String currencyInfo) {
      this.currencyInfo = currencyInfo;
   }
}

code from accepted answer translates to java as following:

First (is not pure immutable):

@JsonAutoDetect(
   fieldVisibility = Visibility.ANY
)
public final class CurrencyInfo {
   @Nullable
   private String currencyInfo;

   @Nullable
   public final String getCurrencyInfo() {
      return this.currencyInfo;
   }

   public final void setCurrencyInfo(@Nullable String var1) {
      this.currencyInfo = var1;
   }

   public CurrencyInfo(@JsonProperty("currency_info") @Nullable String currencyInfo) {
      this.currencyInfo = currencyInfo;
   }
}

Second (probably has problems with deserialization):

public final class CurrencyInfo {
   @Nullable
   private final String currencyInfo;

   @JsonProperty("currency_info")
   @Nullable
   public final String getCurrencyInfo() {
      return this.currencyInfo;
   }

   public CurrencyInfo(@Nullable String currencyInfo) {
      this.currencyInfo = currencyInfo;
   }
}
🌐
Mario Fernandez
hceris.com › painless json with kotlin and jackson
Painless JSON with Kotlin and jackson | Mario Fernandez
June 16, 2019 - However, I like to define a static constructor (using the @JvmStatic and @JsonCreator annotations) so that I can do things like sanitizing the input before generating my instance. This helps to make sure our models are in a consistent state. Since Kotlin 1.3, a new concept called inline classes ...
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 29
Problems deserializing object when default values for constructor parameters are used · Issue #29 · FasterXML/jackson-module-kotlin
May 15, 2016 - I'm having problems deserializing object when when default values for constructor parameters are used. Jackson is attempting to set a parameter that is missing in json to null in Kotlin even though the parameter has a default value. Here's the example that fails: class KotlinJacksonTest { @Test fun testDeserialization() { val mapper = ObjectMapper() mapper.registerModule(KotlinModule()) val test = mapper.readValue("""{"name": "bla"}""", TestObj::class.java) println(test) } } data class TestObj(val name: String, val other: String = "test") Here's the stacktrace: com.fasterxml.jackson.databind.J
Author: FasterXML
🌐
Hacker News
news.ycombinator.com › item
Initialize them with default values, then? Or if you're using Jackson there is a... | Hacker News
March 18, 2017 - lateinit is a crutch to get things working, not something you want to use long-term · It'd be ideal if libraries were better about using constructor injection when possible, but I think there are some valid scenarios where circular dependencies make it impossible to use only constructor injection
🌐
Kotlin Discussions
discuss.kotlinlang.org › t › data-class-and-jackson-annotation-conflict › 397
Data class and jackson annotation conflict - Kotlin Discussions
November 25, 2014 - I have this data class: public data class BluePrint ( JsonProperty("id") val id : Int, JsonProperty("name") val name : String, JsonProperty("content") val content : String) { } and i get this error when trying to serialize it using jackson: ...
🌐
Baeldung
baeldung.com › home › kotlin › kotlin classes and objects › instantiate a kotlin data class using an empty constructor
Instantiate a Kotlin Data Class Using an Empty Constructor | Baeldung on Kotlin
March 19, 2024 - We’ve mentioned that as a data container, Kotlin’s data class brings many advantages. However, one requirement of a data class is that its primary constructor must have at least one parameter. This means it doesn’t have a Java-like “default constructor” or “empty constructor”.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Jackson Fail to Deserialize Request Body due to no Creators, like default constructor, exist in Kotlin Spring - Support - Kotlin Discussions
January 30, 2022 - I received error upon Jackson request deserialization as the destination class written below data class FooRequest(val param1: String) Actually this issue easily solved by add @JsonProperty annotation, also the reason that Jackson needs a no-args constructor is understandable.