kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Provide a default serializer for kotlin.uuid.Uuid class

Open sandwwraith opened this issue 1 year ago • 3 comments

Kotlin 2.0.20 is expected to have a new MPP UUID class (https://youtrack.jetbrains.com/issue/KT-31880, https://github.com/JetBrains/kotlin/commit/25ea5e81af6a1e3638228498f0dea57b7efe5d15). It is reasonable to provide a built-in serializer for it, like we did for kotlin.time.Duration when it was moved to stdlib.

Adding a built-in serializer requires changes in the plugin too, so they have to be made in time, preferably in the same Kotlin 2.0.20 or Kotlin 2.1

sandwwraith avatar Jun 27 '24 12:06 sandwwraith

How do you want to serialize it for different formats? In string formats, like JSON, properties, xml etc., it is common to use the string representation, but for binary formats, a compact binary (using the two longs directly) would be preferred than encoding the string.

hfhbd avatar Jun 27 '24 15:06 hfhbd

Good question. Initially, I was planning to add only String representation, but maybe binary serializer is also required as an additional one

sandwwraith avatar Jun 27 '24 16:06 sandwwraith

Apparently, there is no standardized way of storing UUIDs in protobuf (https://github.com/protocolbuffers/protobuf/issues/2224, https://groups.google.com/g/protobuf/c/THxuTQKsz54). Various implementations use string, bytes, or a pair of fixed64/uint64.

For CBOR, there is tag 37 for UUID byte string (https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml), which is an accepted extension described here: https://github.com/lucas-clemente/cbor-specs/blob/master/uuid.md

So it looks like, for now, only CBOR requires additional handling.

sandwwraith avatar Jun 27 '24 17:06 sandwwraith

On the avro binary format, the uuid is officially represented as a string as said by the spec (the default official uuid type), but is also usually serialized as a fixed of 16 bytes to save space (36 characters/bytes vs 16 bytes), so we may also want to handle a uuid differently depending on the user config.

EDIT: Btw, this is the same for kotlin.time.Duration where the official representation is a fixed of 12 bytes

Chuckame avatar Jul 02 '24 23:07 Chuckame

Well, in the case of format-specific serializers, you can always mark Duration or Uuid with @Contextual and provide a module for Avro/Cbor. If SerializersModule is empty, a default serializer will be used for e.g. Json.

sandwwraith avatar Jul 05 '24 10:07 sandwwraith

Just raised a ticket on the former de-facto uuid implementation for Kotlin with some findings about binary serialization, if anyone is interested: https://github.com/hfhbd/kotlinx-uuid/issues/422

bcmedeiros avatar Dec 14 '24 12:12 bcmedeiros