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.

@JsonProperty annotation ignored

See original GitHub issue

I have the following code. It is in Kotlin, which may be relevant, but I don’t think so:

class WordCounts private @JsonCreator constructor(
        @JsonProperty("sourceToken") val sourceToken: ValueToken<List<String>>,
        @JsonProperty("resultId") resultId: ValueIdGroup<Int>,
        @JsonProperty("wordMap") val wordMap: MutableMap<String, Int>) : LearningTransform {
    // bunch more code here that's not relevant
}

And I have a serialized object that looks like this:

{"sourceToken":{"id":{"name":"words","clazz":"java.util.List"}},"resultId":{"prefix":"wordCounts","clazz":"java.lang.Integer"},"wordMap":{"foo":0,"bar":1,"baz":2}}

When I try to deserialize it I get:

 com.fasterxml.jackson.databind.JsonMappingException: Argument #1 of constructor [constructor for com.analyticspot.ml.framework.testutils.WordCounts, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
     at [Source: {"sourceToken":{"id":{"name":"words","clazz":"java.util.List"}},"resultId":{"prefix":"wordCounts","clazz":"java.lang.Integer"},"wordMap":{"foo":0,"bar":1,"baz":2}}; line: 1, column: 1]
        at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:305)
        at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:268)
        at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
        at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
        at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:476)
        at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3899)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3794)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
        at com.analyticspot.ml.framework.serialization.GraphSerDeserTest.testTokenGroupsSerialize(GraphSerDeserTest.kt:166)

Which seems odd since there is a @JsonProperty annotation on all the values.

Note that ValueIdGroup serializes and deserializes fine by itself (I have unit tests for that).

I also tried adding a static factory method annotated with @JsonCreate and the @JsonProperty annotations:

class WordCounts private constructor(
        val sourceToken: ValueToken<List<String>>,
        resultId: ValueIdGroup<Int>,
       val wordMap: MutableMap<String, Int>) : LearningTransform {

    companion object {
        private val log = LoggerFactory.getLogger(WordCounts::class.java)

       @JvmStatic @JsonCreator
        fun createFromSerialized(
                @JsonProperty("sourceToken") sourceToken: ValueToken<List<String>>,
                @JsonProperty("resultId") resultId: ValueIdGroup<Int>,
                @JsonProperty("wordMap") wordMap: MutableMap<String, Int>): WordCounts {
            return WordCounts(sourceToken, resultId, wordMap)
        }
    }

   // other irrelevant code
}

but the result is the same. In desperation I also tried rewriting ValueIdGroup a few ways thinking maybe that was somehow confusing it and the error message was just wrong but that didn’t help either.

I’ve reproduced this with Jackson 2.7.0, 2.7.4, and 2.8.4 with and without com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4

Am very stuck. Any help would be much appreciated.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
fruedaCodecommented, Apr 27, 2018

It works if I use @field:JsonProperty like this:

data class DataView(val id: String,
                    @field:JsonProperty("dayOfMonth") val monthDay: MonthDay)

data class MonthDay(val day: Int, @field:JsonProperty("asfasdf") val isSunny: Boolean)
0reactions
BartoszKaszewczukcommented, Jun 7, 2018

I also observed the same behaviour but I’m almost certain the issue is with Kotlin’s data class.

I had properties starting with is eg isAlive and the prefix is would always be trimmed during marshalling resulting in JSON property named alive and the discrepancy in field names would then of course make Jackson fail during unmarshalling. @JsonProperty("isAlive") seemed to have no effect but @field:JsonProperty("isAlive") like @fruedaCode suggested did work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson object mapper how to ignore JsonProperty annotation?
To ignore all annotations the syntax in Jackson version 2.x is: objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false).
Read more >
Why @JsonProperty annotation can be ignored? #23 - GitHub
There is no effect. @JsonProperty annotation is being ignored.
Read more >
How to ignore JSON property using Jackson annotations?
In this example you will know how to play with json property names using Jackson annotation @JsonProperty. Sometimes POJOs contain properties that you...
Read more >
Jackson Ignore Properties on Marshalling - Baeldung
We can ignore specific fields at the class level, using the @JsonIgnoreProperties annotation and specifying the fields by name:
Read more >
Jackson annotations ignored in Liberty - Forums - IBM Support
We have been using Jackson Annotations e.g. @Json Serialize(include = Inclusion.ALWAYS) to get NULL values in our JSON Objects. It works well on...
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