https://kotlinlang.org logo
Title
t

tim

11/23/2021, 4:18 PM
Apologies if this is in the worng channel ... I'm using kotlinx.serialization and
@Contextual
to de(serialize) Instant as a Long for my ktor server and I want to de(serialize) it as a Mongo Date for db operations. I am getting the expected behaviour when updating my document, but when I
insertOne
the documents instant is being serialized as a Long not a date (L22). When I later run
updateOne
the instant is being correctly serialised (L28). Any idea what I'm doing wrong here?
Here's my simple test:
Here's where I'm adding the contextual serializers for ktor
Hmm oddly I have another Json instance somewhere else and that appears to be the cause of the issue ... not really sure why its effecting it though 🤔
i

Ivan Brko

11/23/2021, 5:07 PM
i had a similar issue not so long ago and wrote down what caused it for me, so you can take a look
🙏 1
t

tim

11/24/2021, 8:43 AM
I've got it to work as well but what's missing in my mental model, is where kmongo's instance of Json comes from? I explicitly configure contextual serializers for ktor, and so i understand why
call.recieve<T>()
can (de)serialize Instant <--> EpochMilliLong. But I don't see how to specify the contextual serializer for kmongo? Perhaps the
Json { ... }
configures a global singleton and so the ktor configuration block applies to both but then in that instant I would expect kmongo to serialize it as epoch milli 🤷 So how i have it working currently, is my ktor is configured as obove, I don't do anything to kmongo, and then I tag Instant as
@Contextual
which results in the desired behaviour
Okay dug into the kmongo code a bit more and see how the
registerSerializer
function registers a contextual serializer on the kmongo side ... so now have the desired functionality with kotlinx.datetime where ktor treats it as a string and kmongo treats it as a bson date time 🥳