jackson-module-kotlin icon indicating copy to clipboard operation
jackson-module-kotlin copied to clipboard

camelCased property not properly serialized

Open nilswieber opened this issue 3 years ago • 2 comments

Describe the bug An object with a camelCased property, which starts with a single lowercase character isn't properly serialized.

To Reproduce

fun main(args: Array<String>) {
    val mapper = jacksonObjectMapper()

    val serialized = mapper.writeValueAsString(Test())
    println(serialized)
    val deserialized = mapper.readValue<Test>(serialized)
}

class Test {
    val cCase = 1
}

Expected behavior The property should be correctly serialized as cCase but it is serialized as ccase.

So the provided code snippet prints the string {"ccase":1} instead of {"cCase":1}

Because of this behavior the deserialization fails with the following error:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ccase" (class de.nilswieber.Test), not marked as ignorable (0 known properties: ])
 at [Source: (String)"{"ccase":1}"; line: 1, column: 11] (through reference chain: de.nilswieber.Test["ccase"])
	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:1127)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:2023)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1700)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1678)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:319)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:176)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3629)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3612)
	at de.nilswieber.MainKt.main(Main.kt)

Versions Kotlin: Jackson-module-kotlin: 2.13.3 Jackson-databind: 2.13.3

Additional context This issue is likely related to #172 and #173

nilswieber avatar Jun 02 '22 16:06 nilswieber

One workaround would be to annotate the property with @get:JvmName("getcCase") like so:

class Test {
    @get:JvmName("getcCase")
    val cCase = 1
}

This causes Jackson to recognize the field in the correct way.

nilswieber avatar Jun 02 '22 22:06 nilswieber

@nilswieber Or @JsonProperty("cCase")

cowtowncoder avatar Jun 02 '22 22:06 cowtowncoder

The issue of property names in the serialization result differing from the definition in Kotlin will be addressed in #630. This issue be closed as a duplicate.

k163377 avatar Mar 03 '23 15:03 k163377