https://kotlinlang.org logo
#kmongo
Title
# kmongo
m

Mark Vogel

02/17/2023, 12:46 AM
I'm attempting to use Kotlinx Serialization with KMongo and I can't seem to get the serialization to work properly; It does insert the data, but completely ignores all of the Kotlinx annotations. I see that, on the KMongo Website I'm supposed to create a custom
Json
configuration, but I can't seem to find any mention of where to use that
Json
configuration. The closest I see is the reference to registerModule and registerSerializer, but I can't find those functions anywhere either. I've included a screenshot of all the dependencies I have. Note these are all from:
Copy code
implementation("org.litote.kmongo:kmongo-id-serialization:$kMongoVersion")
implementation("org.litote.kmongo:kmongo-coroutine-serialization:$kMongoVersion")
If someone could point me in the right direction I would appreciate it very much! 🙂
c

CLOVIS

02/17/2023, 8:29 AM
Hi! You don't need to create a custom Json configuration. If you're happy with the defaults (and I have not had an issue with them so far), you don't have to do anything.
Do you apply the serialization plugin to your module?
Here's an example of the working setup for KMongo with Coroutines and Serialization: https://gitlab.com/opensavvy/formulaide/-/blob/main/mongo/build.gradle.kts
The essential parts are:
Copy code
plugins {
    kotlin("jvm")

    // Enable the serialization plugin (generates the serializers from @Serializable)
    kotlin("plugin.serialization")
}

dependencies {
    // Coroutines
   
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:<insert version here>")

    // KMongo with coroutines + serialization support
    implementation("org.litote.kmongo:kmongo-coroutine-serialization:<version here>")
}
This is all you need
m

Mark Vogel

02/17/2023, 3:37 PM
@CLOVIS looks like I didn't have the
kotlinx-coroutines-core
which is essential, but doesn't throw errors. Thank you very much! I have a couple other random questions that you may know the answers to: 1. Do you know how to use a custom Json configuration if I have the need? 2. Is it correct to do a
val id: UUID
directly as
_id
or should it be
val id: Id<UUID>
?
c

CLOVIS

02/17/2023, 3:48 PM
1. I do not, but I've been using KMongo for the past 2 years in production and have not needed it, so I believe it's only useful in very rare cases. I believe the likelihood that you're doing something wrong is higher than the likelihood to need it. 2.
Id<UUID>
is a MongoDB ID (e.g.
580f18a2c47a16a1d0b94a80
), the type parameter does nothing in MongoDB (it's just here to help you remember what corresponds to what, see my example below);
UUID
stores the UUID directly in the database. Which one you want depends on what you want to do. For primary keys, it's recommended to use
Id<YourType>
(that's what MongoDB is built for). Don't forget to add
@SerialName
to your primary key!
Copy code
@Serializable
class Foo {
    @SerialName("_id") val id: Id<Foo>, // ID to itself
    val name: String,
    val bar: Id<Bar>, // ID to the other class
}
The type parameter in
bar
does nothing, it's just here to help you remember which is which, you could write
Id<Nothing>
everywhere and it would change nothing (though I don't recommend doing that, it's good to clarify)
m

Mark Vogel

02/17/2023, 5:18 PM
I see that makes sense. Thanks for the info!
So I tested this out a bit and turns out you do need to do
@Contextual @SerialName("_id") val id: UUID
directly rather than using
Id<UUID>
. If you use
Id<UUID>
then it generates an
ObjectId
instead. I think this is like you said that it completely ignores the type parameter. I'm not sure if I did something wrong and that I am supposed to still use
Id<..>
, but it seems to work with the direct
UUID
type.
53 Views