Skip to content

Commit

Permalink
Add basic UuidSerializer until a new kotlinx-serialization release (#385
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hfhbd authored Aug 18, 2024
1 parent d1aa80b commit 320f663
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 9 deletions.
15 changes: 15 additions & 0 deletions kotlinx-uuid-core/api/android/kotlinx-uuid-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ public final class app/softwork/uuid/BinarySerializer : kotlinx/serialization/KS
public static final field INSTANCE Lapp/softwork/uuid/BinarySerializer;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lkotlin/uuid/Uuid;
public fun equals (Ljava/lang/Object;)Z
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public fun hashCode ()I
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lkotlin/uuid/Uuid;)V
public fun toString ()Ljava/lang/String;
}

public final class app/softwork/uuid/NameBasedGeneratorKt {
Expand Down Expand Up @@ -45,6 +48,18 @@ public final class app/softwork/uuid/UuidParceler : kotlinx/parcelize/Parceler {
public fun write (Lkotlin/uuid/Uuid;Landroid/os/Parcel;I)V
}

public final class app/softwork/uuid/UuidSerializer : kotlinx/serialization/KSerializer {
public static final field INSTANCE Lapp/softwork/uuid/UuidSerializer;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lkotlin/uuid/Uuid;
public fun equals (Ljava/lang/Object;)Z
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public fun hashCode ()I
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lkotlin/uuid/Uuid;)V
public fun toString ()Ljava/lang/String;
}

public final class app/softwork/uuid/Uuidv7Kt {
public static final fun Uuidv7 (JLkotlin/random/Random;)Lkotlin/uuid/Uuid;
public static final fun getUnixTimeStamp (Lkotlin/uuid/Uuid;)J
Expand Down
15 changes: 15 additions & 0 deletions kotlinx-uuid-core/api/jvm/kotlinx-uuid-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ public final class app/softwork/uuid/BinarySerializer : kotlinx/serialization/KS
public static final field INSTANCE Lapp/softwork/uuid/BinarySerializer;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lkotlin/uuid/Uuid;
public fun equals (Ljava/lang/Object;)Z
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public fun hashCode ()I
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lkotlin/uuid/Uuid;)V
public fun toString ()Ljava/lang/String;
}

public final class app/softwork/uuid/NameBasedGeneratorKt {
Expand Down Expand Up @@ -32,6 +35,18 @@ public final class app/softwork/uuid/UuidKt {
public static final fun toUuidOrNull (Ljava/lang/String;)Lkotlin/uuid/Uuid;
}

public final class app/softwork/uuid/UuidSerializer : kotlinx/serialization/KSerializer {
public static final field INSTANCE Lapp/softwork/uuid/UuidSerializer;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lkotlin/uuid/Uuid;
public fun equals (Ljava/lang/Object;)Z
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public fun hashCode ()I
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lkotlin/uuid/Uuid;)V
public fun toString ()Ljava/lang/String;
}

public final class app/softwork/uuid/Uuidv7Kt {
public static final fun Uuidv7 (JLkotlin/random/Random;)Lkotlin/uuid/Uuid;
public static final fun getUnixTimeStamp (Lkotlin/uuid/Uuid;)J
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlin.uuid.Uuid
* Cbor.encodeToByteArray(BinarySerializer, myUUID)
* ```
*/
public object BinarySerializer : KSerializer<Uuid> {
public data object BinarySerializer : KSerializer<Uuid> {
private val serializer = LongArraySerializer()
override val descriptor: SerialDescriptor = serializer.descriptor

Expand All @@ -33,12 +33,11 @@ public object BinarySerializer : KSerializer<Uuid> {
}

override fun deserialize(decoder: Decoder): Uuid {
return decoder.decodeSerializableValue(serializer).let { array ->
if (array.size != 2) {
throw SerializationException("Uuid array should consist of 2 elements")
}

Uuid.fromLongs(array[0], array[1])
val array = decoder.decodeSerializableValue(serializer)
if (array.size != 2) {
throw SerializationException("Uuid array should consist of 2 elements")
}

return Uuid.fromLongs(array[0], array[1])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app.softwork.uuid

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.uuid.Uuid

/**
* This is the default [Uuid] serializer that encodes instances as primitive strings
* consisting of the canonical UUID string format.
*/
public data object UuidSerializer : KSerializer<Uuid> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlin.uuid.Uuid", PrimitiveKind.STRING)

override fun serialize(encoder: Encoder, value: Uuid) {
encoder.encodeString(value.toString())
}

override fun deserialize(decoder: Decoder): Uuid {
return Uuid.parse(decoder.decodeString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package app.softwork.uuid

import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlin.test.*
import kotlin.uuid.Uuid

@ExperimentalSerializationApi
class UuidSerializationTest {
@Test
fun smokeTest() {
val value = Uuid.parse(SOME_UUID_STRING)
val encoded = Json.encodeToString(UuidSerializer, value)
assertEquals("\"$SOME_UUID_STRING\"", encoded)
val decoded = Json.decodeFromString(UuidSerializer, encoded)

assertEquals(value, decoded)
}
}
6 changes: 6 additions & 0 deletions kotlinx-uuid-sqldelight/api/kotlinx-uuid-sqldelight.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ public final class app/softwork/uuid/sqldelight/UuidByteArrayAdapter : app/cash/
public fun decode ([B)Lkotlin/uuid/Uuid;
public synthetic fun encode (Ljava/lang/Object;)Ljava/lang/Object;
public fun encode (Lkotlin/uuid/Uuid;)[B
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class app/softwork/uuid/sqldelight/UuidStringAdapter : app/cash/sqldelight/ColumnAdapter {
Expand All @@ -12,5 +15,8 @@ public final class app/softwork/uuid/sqldelight/UuidStringAdapter : app/cash/sql
public fun decode (Ljava/lang/String;)Lkotlin/uuid/Uuid;
public synthetic fun encode (Ljava/lang/Object;)Ljava/lang/Object;
public fun encode (Lkotlin/uuid/Uuid;)Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app.softwork.uuid.sqldelight
import app.cash.sqldelight.*
import kotlin.uuid.Uuid

public object UuidByteArrayAdapter : ColumnAdapter<Uuid, ByteArray> {
public data object UuidByteArrayAdapter : ColumnAdapter<Uuid, ByteArray> {
override fun decode(databaseValue: ByteArray): Uuid = Uuid.fromByteArray(databaseValue)
override fun encode(value: Uuid): ByteArray = value.toByteArray()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app.softwork.uuid.sqldelight
import app.cash.sqldelight.*
import kotlin.uuid.Uuid

public object UuidStringAdapter : ColumnAdapter<Uuid, String> {
public data object UuidStringAdapter : ColumnAdapter<Uuid, String> {
override fun decode(databaseValue: String): Uuid = Uuid.parse(databaseValue)
override fun encode(value: Uuid): String = value.toString()
}

0 comments on commit 320f663

Please sign in to comment.