Usage of Jackson @JsonProperty annotation for kotlin data classes
Asked Answered
C

9

68

kotlin 1.2.10 jackson-module-kotlin:2.9.0

I have the following data class in kotlin:

data class CurrencyInfo(
        @JsonProperty("currency_info") var currencyInfo: CurrencyInfoItem?
)

@JsonInclude(JsonInclude.Include.NON_NULL)
data class CurrencyInfoItem(
        @JsonProperty("iso_4217") var iso4217: String?,
        @JsonProperty("name") var name: String?,
        @JsonProperty("name_major") var nameMajor: String?,
        @JsonProperty("name_minor") var nameMinor: String?,
        @JsonProperty("i_ma_currency") var iMaCurrency: Int?,
        @JsonProperty("i_merchant_account") var iMerchantAccount: Int?,
        @JsonProperty("i_x_rate_source") var iXRateSource: Int?,
        @JsonProperty("base_units") var baseUnits: Double?,
        @JsonProperty("min_allowed_payment") var minAllowedPayment: Int?,
        @JsonProperty("decimal_digits") var decimalDigits: Int?,
        @JsonProperty("is_used") var isUsed: Boolean?
)

When I try to deserialize this data class I get the following:

{"currency_info":{"iso_4217":"CAD","name":"Canadian Dollar","imerchantAccount":0,"ixrateSource":2}}

As you can see, the last two options were deserialized incorrectly. This issue could be solved by adding directly annotation to getter @get:JsonProperty. However, according to jackson docs @JsonProperty should be assigned to getters/setters/fields

So, I want to ask is there a reliable way to annotate property for jackson in kotlin to have correct serialization/deserialization (moreover all my data classes are autogenerated, so it would be hard to create some two/three lines annotations, separately for getter and setter)

Otherwise, could this issue be resolved by some jackson settings?

According to answers below, the following works for me

private val mapper = ObjectMapper().registerKotlinModule()
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
Chinch answered 26/12, 2017 at 18:8 Comment(1)
you need just need .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) .setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE). Issue depends on specific handling of "is" gettersThamos
M
99

@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?
)
Marconigraph answered 26/12, 2017 at 23:46 Comment(7)
Thanks, I set the visibility modifier globallyChinch
I have jackson-annotations-2.9.0 and there is no PRIVATE option. I tried with ANY, but the property is still ignored. View Source codeCornea
The answer down below that uses @param:JsonProperty("") is also critical if you're trying to replicate this for something that doesn't happen when you are getting the value, i.e. custom Json Deserializers that happen on construction.Upcast
not working for me any of thisCoheman
Hi, in Java it is possible to have different jsonproperty names while serializing and deserializing. Is it possible to do similar thing for fields in Kotlin data class? I see @get:JsonProperty but not @set:Jsonproperty. #8560848Pox
@Pox yes, there's a set target for kotlin annotations: kotlinlang.org/docs/…Marconigraph
I tried giving 2 different jsonProperty names to @get and @set but then I get the error Conflicting/ambiguous property name definitions.... found multiple explicit namesPox
M
21

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;
   }
}
Malay answered 4/3, 2019 at 6:11 Comment(0)
G
17

You can add the jackson-module-kotlin (https://github.com/FasterXML/jackson-module-kotlin) and register the kotlin module with jackson:

val mapper = ObjectMapper().registerKotlinModule()

Then it (and many other things) works automagically.

Giselagiselbert answered 12/10, 2018 at 10:30 Comment(5)
It was used from the very beginning. You could even check the initialization in the question post.Chinch
@Chinch yeah, right I missed that one. It however did the job for me.Giselagiselbert
By far it is the best answer here: registering this module not only resolves the issue without needing to type @get: or use other tweaks, but improves Kotlin<>Jackson interop overall.Spirograph
KotlinModule() does not in the last version of jackson unfortunately but all docs talk about use of KotlinModule() unfortunately. nobody says how to do that in latest versionMurat
@Murat see github.com/FasterXML/jackson-module-kotlin#usage: ObjectMapper().registerKotlinModule()Giselagiselbert
D
6

Kotlin doesn't support @param and @get annotations as one annotation, so we have to write such code:

data class User(
    @param:JsonProperty("id") @get:JsonProperty("id") val id: Int,
    @param:JsonProperty("name") @get:JsonProperty("name") val name: String
)

Here you can tell JetBrain guys to support this feature and allow:

data class User(
    @JsonProperty("id") val id: Int,
    @JsonProperty("name") val name: String
)

https://youtrack.jetbrains.com/issue/KT-11005

Downtrodden answered 9/6, 2020 at 13:9 Comment(0)
M
4

You can configure the ObjectMapper from the jackson library by calling the method setPropertyNamingStrategy(...)

Using PropertyNamingStrategy.SNAKE_CASE should resolve your problem

See also the other available strategies here : PropertyNamingStrategy

Microcopy answered 26/12, 2017 at 18:31 Comment(1)
Thanks, it's a good idea, I have tried it. But default SNAKE_CASE translation has a poor implementation. A lot of fields are resolved incorrectly. For example, iso4217 -> not translated into iso_4217, iXRateSource -> i_x_rate_source and so onChinch
P
3

this issue is resolved in

https://github.com/FasterXML/jackson-module-kotlin/issues/237

or you can also use

data class SignRequest    
    @param:JsonProperty("estamp_request")
    @get:JsonProperty("estamp_request")
    val eStamp: EstampRequest?
}

data class EstampRequest(
    val tags: Map<String,Int>
)
Patois answered 2/3, 2021 at 8:39 Comment(1)
As far as I see this issue is NOT resolved at the time of my writing, but there are some workarounds in the comments.Extra
C
1

I faced this problem today and what I did was register KotlinModule() in my ObjectMapper(). Bellows follow an example.

@Bean fun objectMapper = ObjectMapper().registreModule(KotlinModule())

Above it's a dummy objectMapper, I believe that you should put other configurations in your objectMapper like serializers and so on

Chinoiserie answered 20/3, 2020 at 15:16 Comment(0)
P
0

In my case works only this mapper:

    @Bean
    fun objectMapper(): ObjectMapper =
        jacksonObjectMapper()
            .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

with @JsonProperty on each field (even on one word named) and default constructor

Pforzheim answered 13/11, 2023 at 10:7 Comment(0)
D
0

This bug is causing problems with JsonProperty and Kotlin- https://youtrack.jetbrains.com/issue/KT-67977 If using Kotlin, you must do @field:JsonProperty("yourPropertyHere").

Dorking answered 16/7, 2024 at 16:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.