Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Jackson @JsonProperty annotation for kotlin data classes

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) 
like image 439
Mike Avatar asked Dec 26 '17 18:12

Mike


People also ask

What is the use of @JsonProperty annotation?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

What is Jackson annotations used for?

Jackson Annotations - @JsonAnyGetter @JsonAnyGetter allows a getter method to return Map which is then used to serialize the additional properties of JSON in the similar fashion as other properties.

What is Jackson in Kotlin?

An introduction to Jackson Kotlin module and Kotlin nullable type for missing values in data. Jackson is one of the famous library to parse XML or JSON data though with Kotlin, some key factors are required to avoid unexpected issues.

Where can I find the source code for Kotlin’s JSON annotations?

We also learned how to use @JsonProperty and @JsonInclude annotations. And, as always, the full source code can be found over on GitHub. If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Is @jsonproperty a class level annotation?

} @JsonProperty is not class level annotation, and you don't need to mark your class with any annotation. If you provide your class name as an argument to parser it will know how to map it according to your getter methods. It is as if every getter method has been marked with @JsonProperty without any argument

Does Jackson work with Kotlin?

In this tutorial, we’ll discuss the Jackson support for Kotlin. We’ll explore how to serialize and deserialize both Object s and Collection s. We’ll also make use of @JsonProperty and @JsonInclude annotations.

What is @jsonalias annotation in Jackson?

On this page we will provide Jackson @JsonProperty and @JsonAlias annotation example. @JsonProperty defines a logical property used in serialization and deserialization of JSON. When we set JSON data to Java Object, it is called JSON deserialization and when we get JSON data from Java Object, it is called JSON serialization.


1 Answers

@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? ) 
like image 122
SimY4 Avatar answered Sep 23 '22 07:09

SimY4