FasterXML / jackson-module-kotlin

Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.
Apache License 2.0
1.12k stars 175 forks source link

Need @JsonIgnore to deserialize kotlin data-class. kotlin-module 2.10.0 #268

Closed joachimbjorklund closed 4 years ago

joachimbjorklund commented 4 years ago

Hi given the following test, will break with:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `basis.test.api_clients.common.DataTest$Data` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('testing')
 at [Source: (String)"{"data": "testing"}"; line: 1, column: 10]
class DataTest {

    data class Data(val value: String)
    data class DataWithData(val data: Data)

    @Test
    fun testDeserialize() {
        val json = """{"data": "testing"}"""

        val dataWithData = ObjectMapper().registerKotlinModule().readValue(json, DataWithData::class.java)
        Assert.assertEquals(dataWithData.data.value, "testing")
    }
}

The only way (I found) is to annotate the Data class with @JsonIgnore like this: data class Data(@JsonIgnore val value: String)

Am I missing something here?

207

rgds Joachim

apatrida commented 4 years ago

You are asking a support question which should really be asked on Stack Overflow, this is not a bug, but rather an incorrect use of how you have annotated your classes. Check out the JsonCreator annotation with the mode to delegate, or ask on Stack Overflow. thanks!