Usage of Jackson @JsonProperty annotation for kotlin data classes

后端 未结 6 1227
野趣味
野趣味 2020-12-09 07:14

kotlin 1.2.10 jackson-module-kotlin:2.9.0

I have the following data class in kotlin:

data class CurrencyInfo(
        @JsonProperty(\"currency_info\"         


        
相关标签:
6条回答
  • 2020-12-09 07:59

    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

    0 讨论(0)
  • 2020-12-09 08:02

    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

    0 讨论(0)
  • 2020-12-09 08:06

    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

    0 讨论(0)
  • 2020-12-09 08:08

    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;
       }
    }
    
    0 讨论(0)
  • 2020-12-09 08:14

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

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

    Then it (and many other things) works automagically.

    0 讨论(0)
  • 2020-12-09 08:15

    @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?
    )
    
    0 讨论(0)
提交回复
热议问题