Closed Bixilon closed 2 months ago
I do not plan to implement setter support for value class
at this time.
The reasons are as follows
kotlin-module
as more reflection processing would be required.Kotlin
classes is generally done using constructors or factory functions, and there is theoretically no case where you must always use setters.So, I am inlining a class that just contains an int (this improves performance and memory allocation in my application).
I would suggest benchmarking first.
For libraries using kotlin-reflect
, performance will degrade if you use the value class
.
Compared to using data class
, for example, the throughput of deserialization is roughly 70% (see the benchmark results in the link).
https://github.com/FasterXML/jackson-module-kotlin/blob/2.18/docs/value-class-support.md#note-on-the-use-of-value-class
Requires a very complex implementation.
Yes, agreed. I thought that is something already.
I would suggest benchmarking first.
My application does not use jackson heavily, it is more something for reading and writing settings (and a couple of other things). The main improvement comes from memory allocation, the color class is not mutable, and I already started replacing colors with int in a lot of places. The memory allocation goes down by 30-50 MB/s, it makes the gc almost sleep. In my case the jackson sacrifize would be totally worth it (even if it is slower by 10 times) to make the code quality go up and not use ints in the code. But I guess this is only true in my case and a lot of people would not want to take this.
Thanks for the answer, I need to find a different way (maybe use a color wrapper class or so for config files).
The main improvement comes from memory allocation
Hmmm, if that is the case, it might be preferable not to use jackson-module-kotlin
.
I haven't done a comparison, but as far as I know, this module uses more memory because it uses kotlin-reflect
.
If you are looking for performance, since Jackson
supports record classes, you might consider defining DTO
as JvmRecord
and not using kotlinModule
.
Alternatively, kotlinx-serialization
does not use reflection, which may also reduce runtime memory consumption (I believe value class
is also supported).
Search before asking
Describe the bug
So, I am inlining a class that just contains an int (this improves performance and memory allocation in my application).
This inlined class (color) has a custom type deserializer (I am saving it as a hex encoded string with a hashtag in front of it.
The example is crashing with:
The problem is, that the deserializer is not regionized by jackson. Decompiled, the whole construct looks like this:
The getter/setter is acutally called
getDelegate_JaeQ8us
andsetDelegate_5o48kM4
. The custom deserializer is not even called, only when I add@get:JsonDeserialize(using = Deserializer::class) @set:JsonDeserialize(using = Deserializer::class)
That was just the simple case, in real I got a delegate behind it (
by
).To Reproduce
generic code:
direct field:
delegate field:
Expected behavior
The tests should pass
Versions
Kotlin: Jackson-module-kotlin: 2.17.0 Jackson-databind: 2.17.0 kotlin: 2.0 (maybe that makes a difference)
Additional context
No response