Skip to content
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

Supports deserialization of value class (inline class). #650

Closed
k163377 opened this issue Mar 17, 2023 · 5 comments · Fixed by #768
Closed

Supports deserialization of value class (inline class). #650

k163377 opened this issue Mar 17, 2023 · 5 comments · Fixed by #768

Comments

@k163377
Copy link
Contributor

k163377 commented Mar 17, 2023

jackson-module-kotlin currently does not support deserialization of value class.
This issue summarizes information about deserialization support for value class.

About why jackson-module-kotlin has difficulty supporting value class.

The main reason it is difficult to support value class in jackson-module-kotlin is that the bytecodes for the value class and its associated arguments and return values are different than usual.
Jackson is a Java Reflection based library and cannot fully parse such content.
As far as I have tried in the experimental project described below, I have already found that full support is impractical unless an option is provided by Kotlin to ensure compatibility.

Implementation status of value class support.

value class support is being preliminarily implemented in the following experimental project.
At this time, I have confirmed that it works in many major cases, even for deserialization.
https://github.com/ProjectMapK/jackson-module-kogera

The implementation of this project will be incorporated into jackson-module-kotlin in the future.

The following issue also has information.
#199 (comment)

@k163377
Copy link
Contributor Author

k163377 commented Dec 2, 2023

Support for JsonDeserialize and JacksonInject annotations for value class parameters has been confirmed from an experimental implementation in kogera.
https://github.com/ProjectMapK/jackson-module-kogera/releases/tag/2.16.0-beta8

As jackson-module-kotlin 2.17 allows updates to Kotlin 1.7 or later, I will try to introduce value class support.

@k163377
Copy link
Contributor Author

k163377 commented Feb 18, 2024

cross-posting
#199 (comment)

@geirsagberg
Copy link

geirsagberg commented Mar 17, 2024

Deserializing a map where the key is a value class does not work in 2.17 unfortunately:

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

@JvmInline
value class Id(val value: Int)

data class Foo(val valuesById: Map<Id, String>)

val objectMapper = jacksonObjectMapper()

val serialized = objectMapper.writeValueAsString(Foo(mapOf(Id(1) to "one", Id(2) to "two")))

val deserialized = objectMapper.readValue(serialized, Foo::class.java)

Throws:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot find a (Map) Key deserializer for type...

@pschichtel
Copy link

@geirsagberg I guess this would be better reported in a dedicated issue

@geirsagberg
Copy link

Created an issue: #777

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants