question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jackson's @JsonProperty in @JsonCreator methods should not affect Kotlin class properties.

See original GitHub issue

Java 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:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Jan 25, 2018

@get:JsonProperty("user_id") Long id works for me.

2reactions
cowtowncodercommented, Dec 22, 2017

@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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found