对 kotlin 数据类使用 Jackson @JsonProperty 注释 [英] Usage of Jackson @JsonProperty annotation for kotlin data classes

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

问题描述

科特林 1.2.10杰克逊模块科特林: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/setter/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)

否则,这个问题可以通过一些jackson设置来解决吗?

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 代码中的注释都放在数据类中的私有字段上,默认情况下 Jackson 不扫描私有字段用于注释.您必须通过放置 @JsonAutoDetect 注释来指示它做其他事情:

@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?
)

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

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