Jackson's @JsonProperty in @JsonCreator methods should not affect Kotlin class properties.
See original GitHub issueJava version: 1.8.0_144 Kotlin version: 1.1.51
Given a Java class:
public class User {
private final Long id;
private final String name;
@JsonCreator
public User(
@JsonProperty("user_id")
Long id,
@JsonProperty("user_name")
String name) {
this.id = id;
this.name = name;
}
// Getters
}
one is able to deserialize it from json
{"user_id": 1, "user_name": "john"}
and serialize it to
{"id":1, "name": "john"}
However, given the equivalent Kotlin class
class User
@JsonCreator constructor(
@JsonProperty("user_id")
id: Long,
@JsonProperty("user_name")
name: String) {
val id = id
val name = name
}
the serialization result becomes
{"user_id": 1, "user_name": "john"}
(deserialization still works as-is though).
Same thing happens with a static @JsonCreator
method like
class User(val id: Long, val name: String) {
companion object {
@JsonCreator
@JvmStatic
fun create(@JsonProperty("user_id")
id: Long,
@JsonProperty("user_name")
name: String): User {
return User(id, name)
}
}
}
The expectation is that the @JsonProperty
in the constructor or the static factory method would only be effective in the scope of the method; it shouldn’t affect the properties in the class.
What confuses me more is that why the @JsonProperty
annotations in the static @JsonCreator
method would affect the properties in the class…
Please fix the inconsistency.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Painless JSON with Kotlin and jackson - Mario Fernandez
It should be an immutable data class; It should not force a change in the structure of the JSON (i.e., no nesting); Serialize/Deserialize...
Read more >Usage of Jackson @JsonProperty annotation for kotlin data ...
@JsonProperty annotations in your code are all put on private fields within your data class and by default Jackson doesn't scan private ...
Read more >Jackson Support for Kotlin - Baeldung
In this tutorial, we'll discuss the Jackson support for Kotlin. We'll explore how to serialize and deserialize both Objects and Collections.
Read more >Efficient JSON serialization with Jackson and Java
The Jackson open source library for JSON works with many programming languages. ... JavaScript does not natively support classes.
Read more >How to Ignore Unknown Properties While Parsing JSON in ...
Jackson API provides two ways to ignore unknown fields, first at the class level using @JsonIgnoreProperties annotation and second at the ObjectMapper level ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@get:JsonProperty("user_id") Long id
works for me.@HunterSherms since all annotations for all accessors (getter, setter, field, creator argument) are combined, both serialization and deserialization can be affected. So renaming affects the “whole” logical property, not just one direction.
Whether serialization or deserialization is being performed does affect precedence (priority) of annotations, however. So for deserialization, constructor parameters have highest precedence, followed by setter, then by field. For serialization highest precedence by getter.
Hope this helps.