Jackson @JsonProperty注释用于kotlin数据类的用法 [英] Usage of Jackson @JsonProperty annotation for kotlin data classes

查看:1249
本文介绍了Jackson @JsonProperty注释用于kotlin数据类的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

kotlin 1.2.10
jackson-module-kotlin:2.9.0

kotlin 1.2.10 jackson-module-kotlin:2.9.0

我在kotlin中有以下数据类:

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}}

如您所见,最后两个选项被错误地反序列化。
这个问题可以通过直接向getter @get:JsonProperty添加注释来解决。但是,根据jackson docs @JsonProperty应该被分配给getter / setters / fields

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

所以,我想问一下是否有一种可靠的方法来为kotlin中的jackson注释属性要有正确的序列化/反序列化(而且我的所有数据类都是自动生成的,因此很难创建一些两行/三行注释,分别用于getter和setter)

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)


推荐答案

@JsonProperty 代码中的注释全部放在数据类的私有字段中。默认情况下,杰克逊不会扫描私有字段以进行注释。你必须通过输入 JsonAutodetect 注释来指示它做其他事情:

@JsonProperty annotations in your code are all putted 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?
)

这篇关于Jackson @JsonProperty注释用于kotlin数据类的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆