Mark Vogel
02/17/2023, 12:46 AMJson
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:
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! 🙂CLOVIS
02/17/2023, 8:29 AMCLOVIS
02/17/2023, 8:30 AMCLOVIS
02/17/2023, 8:30 AMCLOVIS
02/17/2023, 8:33 AMplugins {
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 needMark Vogel
02/17/2023, 3:37 PMkotlinx-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>
?CLOVIS
02/17/2023, 3:48 PMId<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!
@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)Mark Vogel
02/17/2023, 5:18 PMMark Vogel
02/17/2023, 6:04 PM@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.