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.11k stars 175 forks source link

How to disable the feature who rename a primitive boolean "isXXX" to "XXX" #781

Open SannaK opened 3 months ago

SannaK commented 3 months ago

Your question

Hello,

I know is a feature but it's annoying in my case : how to avoid the serializer to renamme my property "isAvailable" to "available". I want to keep the exact name. The expected resulst should be "is_available" with my settings.

internal fun getPrettyMapper(): ObjectWriter = ObjectMapper()
    .setMixInResolver(object : ClassIntrospector.MixInResolver {
        override fun findMixInClassFor(cls: Class<*>?): Class<*>? {
            return when (cls) {
                AppError::class.java -> AppErrorJson::class.java
                Resource::class.java -> ResourceJson::class.java
                else -> null
            }
        }
        override fun copy() = this
    })
    .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
    .writerWithDefaultPrettyPrinter()

Thanks for your help.

k163377 commented 3 months ago

The following answer relates to the latest version (it would be helpful if you could attach the version information to your explanation).

As I recall, this problem is caused by KotlinModule performing internal processing in a function called findImplicitPropertyName. https://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/latest/com/fasterxml/jackson/databind/AnnotationIntrospector.html#findImplicitPropertyName-com.fasterxml.jackson.databind.introspect.AnnotatedMember-

In other words, the KotlinModule has to wait for this problem to be fixed on the databind side. For individual use cases, a custom AnnotationIntrospector can also be used to fix names across the board (possible side effects, but not likely to be a problem for limited use cases). https://www.javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/latest/com/fasterxml/jackson/databind/AnnotationIntrospector.html

I am very sorry, but I am very busy right now and cannot investigate this in detail.