Skip to content

Don't instantiate new instances of Kotlin singleton objectsΒ #225

@Dico200

Description

@Dico200

Same topic as the problem described by #141, #196, #159.

On #141, @apatrida said:

This is outside the scope of the module, you cannot replace an object singleton in Kotlin, and if it is immutable values the content could not be set. Jackson does not "bind into existing instances" for any case and the definition of readValue is specifically to create new objects created from the contents of the JSON. The Kotlin module only provides information to the data binder and does not control when it decides to create an instance only in how it knows how to create that instance.

By his logic, Jackson should be instantiating enum values too, because they can contain state. But obviously, Jackson doesn't do that, because it enums are singletons by definition.
Kotlin singleton objects fall into the exact same category as enum values. They can contain state, but that state should not be serialized, just like enum values' state. There should only ever be one instance, and Jackson instantiating new ones can break applications.

Jackson does not "bind into existing instances" for any case

Except for enum values. And it should do the same for Kotlin objects, as per my argument above.

It is a very common Pattern in Kotlin to use objects in place of enums because they can be used as constant instances of a sealed class, where other subclasses can hold state because they are instantiable. It is hurting very much that the Jackson Kotlin module cannot handle this properly, because object singletons are not comparable by == operator by default, and programmers expect === to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions