What are y'all using for JSON Serialization? Most ...
# spring
j
What are y'all using for JSON Serialization? Most Spring resources use Jackson. We have also been using Jackson in some of our projects. Recently I've been using Kotlinx Serialization and find it much nicer.
e
Definitely nicer imo and in new projects I will probably replace Jackson entirely with KxS
a
We use Jackson and have been happy with it. I haven't tried kotlinx serialization, but if we have some issues with jackson then I'd try it
r
Could you please define "nicer"?
j
The dictionary has a perfectly fine definition for nicer
comparative form of nice: more nice
If you mean what I prefer? It's adding a
@Serializable
to any data-class and it works out of the box. Jackson requires a few steps more. This could also be a skill-issue on my part
e
I typically prefer KxS. I had issues in the past with Jackson deserializing null into a non-null property. It has been fixed since, but now I’m used to the KxS way so just sticking with it. Also I think Jackson serializes the mangled names for value classes? Only downside imo is that I’ve sometimes been bit by Spring using Jackson even though I intended to use KxS.
1
j
seems annoying to have to go back to annotating everything, when Jackson does it auto-magically?
👍 2
p
I do use KxS for pet projects but for professional projects I’m sticking with Jackson. While KxS is much much faster I prefer to lean towards the mature solutions when it comes to production applications
t
I gave up on KxS when I tried to ser/deser
Any
, but it could have been a skill issue as well (though jackson is quite complex and I don't pretend to be an expert, I feel I can use it efficiently enough). I am planning to give KxS another try in a next project, I need to double check where support in Spring stands but it's definitely an option I want to explore
j
If I have to believe this Reddit comment then Spring will use KxS if you want to https://www.reddit.com/r/Kotlin/comments/y7vx36/comment/isxcms4/?utm_source=share&ut[…]m=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button Providing sources would be tihgt
Kotlin multiplatform serialization is supported in Spring MVC, Spring WebFlux and Spring Messaging (RSocket). The builtin support currently targets CBOR, JSON, and ProtoBuf formats.
To enable it, follow those instructions to add the related dependency and plugin. With Spring MVC and WebFlux, both Kotlin serialization and Jackson will be configured by default if they are in the classpath since Kotlin serialization is designed to serialize only Kotlin classes annotated with
@Serializable
. With Spring Messaging (RSocket), make sure that neither Jackson, GSON or JSONB are in the classpath if you want automatic configuration, if Jackson is needed configure
KotlinSerializationJsonMessageConverter
manually.
j
NICE
p
We have some json fields in postgres database, and we are using it for that.
j
I started writing a blog post comparing Jackson and KxS with a big bias towards KxS. Honestly the latest Jackson + Kotlin is amazing. In my small benchmark it's also faster... 😐
t
you probably want to check memory consumption, jackson is superfast because it relies a lot on caching/memoization to avoid redoing the same work over and over
though in a small benchmark might be difficult to see 😄
j
That's a great point. I am serializing the same object 100k, 1m and 100m times. I'lll have to check memory consumption but also make my objects more random.
t
afaik, jackson is about shape, not content. so changing
{"username": "a"}
to
{"username":"f"}
should have little to no impact, but having another object like
{"size":2 }
should change more in terms of jackson optimization
j
I'm really trying to compare the 2 fairly. I thought Jackson would be much slower due to its use of reflection, but I guess Jackson-Kotlin does some magic?