https://kotlinlang.org logo
#serialization
Title
# serialization
a

Asaf Peleg

10/03/2023, 1:06 AM
I'm running into an issue where logback using
ch.qos.logback.classic.encoder.JsonEncoder
doesn't want to serialize
kotlinx.datetime.LocalDateTime
It seems that the underlying encoder uses Jackson and I'm wondering if I have to pass something special to the encoder to make it work? Can't seem to find any info online about that and wondering if someone has run across this before. Same issue using the joda time equivalents
k

Kyle

10/03/2023, 1:19 PM
I just ran into this. You have to create a custom jackson encoder to use that. I ended up switching everything to using kotlinx-serialization instead so it would use kotlinx-datetime’s included serializers
a

Asaf Peleg

10/03/2023, 2:00 PM
how do you switch the logger to use
kotlinx-serialization
thats built into logback encoder isn't it?
k

Kyle

10/03/2023, 5:18 PM
whoops. I ran into that problem with spring boot and missed that you mentioned logback. I haven’t even looked at logback’s date management. Sorry, can’t help you much there. I’m guessing it’ll be the same thing though, you’ll need to create your own encoder.
a

Asaf Peleg

10/03/2023, 5:36 PM
wowzers thanks
m

Michael Strasser

10/06/2023, 2:38 AM
Or maybe not use Logback? In Spring Boot you can switch in Log4j2, which is arguably superior. Or you can use Klogging with Spring Boot. (I am the author of Klogging.)
a

Asaf Peleg

10/06/2023, 2:58 AM
thanks Michael, I'ma actually running a Ktor server and I've been using https://github.com/oshai/kotlin-logging which I think I picked up from my previous project a few years ago but I'm not certain. I'll give your lib & log4j2 a gander though.
m

Michael Strasser

10/06/2023, 4:39 AM
Klogging can be used easily with Ktor as well. I checked: Klogging serialises Kotlin LocalDateTime values without problem. Let me know how you go or if you have any questions.
a

Asaf Peleg

10/06/2023, 5:00 AM
but for outputting JSON won't it still rely on the internal logging encoder?
m

Michael Strasser

10/06/2023, 5:05 AM
Klogging uses its own, simple JSON serialisation for log output. I started with that and haven’t need to change it so far. For
LocalDateTime
it calls
toString()
that renders in ISO8601 format. (It uses KotlinX Serialization to parse JSON configuration files.)
a

Asaf Peleg

10/06/2023, 3:50 PM
roger
3 Views