Skip to content

UuidAsBsonBinarySerializer

Serializer for kotlin.uuid.Uuid that serializes as a BsonType.BinaryData with subtype 4 (UUID).

The MongoDB official driver's KotlinX.Serialization support doesn't yet support kotlin.uuid.Uuid (see JAVA-6083). This serializer adds this support. Once this ticket is fixed, this serializer will be deprecated.

When KtMongo Multiplatform's BSON implementation is used, this serializer does nothing, as the default serializer already uses BsonType.BinaryData.

Behavior with non-BSON formats

When serializing to non-BSON formats (e.g., JSON, gRPC…), the UUID is encoded as a string in its standard hyphenated format (e.g., 550e8400-e29b-41d4-a716-446655440000).

Example

The annotation UseSerializers can be used to configure all Uuid fields in that file to use this serializer:

@file:UseSerializers(UuidAsBsonBinarySerializer::class)

@Serializable
data class Foo(
    val _id: ObjectId,
    val correlationId: Uuid,
)

Alternatively, Serializable can be used to configure a specific Uuid field to use this serializer:

@Serializable
data class Foo(
    val _id: ObjectId,
    val correlationId: @Serializable(with = UuidAsBsonBinarySerializer::class) Uuid,
)

Properties

descriptor

open override val descriptor: SerialDescriptor

Functions

deserialize

open override fun deserialize(decoder: Decoder): Uuid

serialize

open override fun serialize(encoder: Encoder, value: Uuid)