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

当我try 反序列化这个数据类时,我得到以下结果:

{"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)

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

根据下面的回答,以下方法对我有效

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

或者,您也可以移动访问器方法上的注释:

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

Json相关问答推荐

使用Jolt库对多个数组进行嵌套循环

过go 24小时内判断员事件的EventBridge事件模式

在AWS步骤函数中将字符串解析为JSON&S映射状态

nlohmann json:为什么我会得到一个解析错误?

交换键和数组值,将旧键转换为新数组值,使用 jq

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

如何编写 jolt 规范以将不同的对象转换为数组

try 使用 JQ 转换 JSON 中过于复杂的对象数组

将来自 Golang 的 JSON API 调用响应输出到 nextjs 前端

我无法将来自 API 的数据显示到 FlatList 中,但我在 console.log 中看到了它.问题是什么?

Powershell - 如何从 JSON 中删除/过滤元素但保留其余部分?

如何在 Postman 中匹配 json 响应中的内容?并可视化

ORA-01422: 精确提取返回的行数超过了与 json 对象组合的请求数

传统编程语言等价于动态 SQL

在 Flutter 中将对象转换为可编码对象失败

在 JSON 字符串中react i18n 换行符

如何使用 Json.NET 反序列化可以是两种不同数据类型的 JSON 属性

类型是接口或抽象类,不能实例化

使用 JSON.NET 序列化/反序列化对象字典

log4j 支持 JSON 格式吗?