-
-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserializing object declarations does not preserve identity #147
Comments
FYI, right now I'm using a workaround to have comparisons work as expected by overriding the sealed class Purpose {
object StockOnHand : Purpose() {
// Hack for dealing with comparing objects after being JSON deserialized
override fun equals(other: Any?): Boolean {
return other is StockOnHand
}
}
data class Fulfillment(val channel: String) : Purpose()
} |
Another workaround, provided your fun <T : Any> T.normalize(): T {
this::class.objectInstance?.takeUnless { it === this }?.let { return it }
return this
} |
@nanodeath certainly an option, might make it even easier to generically fix the issue in the json parsing code in case I introduce other objects later. |
I'm not sure it is safe to have the Kotlin module deserialize something into the "one true instance" when it is an object, what if the contents are not the same? how would we even check that? The |
Object declarations are singletons, so only one instance of these elements should be generated on a single JVM. |
I also have this problem. @EdeMeijer
|
If you have reached here thinking that there is a regression bug just notice that from version 2.11.0 onwards the behaviour has been disabled by default and needs to be activated with something like:
|
... or, in newer versions: mapper.registerModule(
kotlinModule {
configure(KotlinFeature.SingletonSupport, true)
}
) |
When serializing and unserializing an object declaration, the resulting instance has a different identity than the original one, resulting in the objects being considered non-equal. For example:
I would expect Jackson to return the one and only singleton instance that Kotlin created for the object and have behaviour similar to data classes.
You might wonder why I'd try to JSON serialize an object declaration. My use case is dealing with polymorphism having a sealed class with 2 implementation; one is a data class and the other is an object, like this:
which translates to JSON like
or
The text was updated successfully, but these errors were encountered: