Skip to content

Commit

Permalink
Add UuidSerializer as a standard serializer to kotlinx-serialization …
Browse files Browse the repository at this point in the history
…plugin

UuidSerializer should appear in 1.7.1.

Related ticket: Kotlin/kotlinx.serialization#2730
  • Loading branch information
sandwwraith committed Jul 12, 2024
1 parent 3514a5c commit dd3d59d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ fun findStandardKotlinTypeSerializerName(typeName: String?): String? {
"kotlin.DoubleArray" -> "DoubleArraySerializer"
"kotlin.BooleanArray" -> "BooleanArraySerializer"
"kotlin.time.Duration" -> "DurationSerializer"
"kotlin.uuid.Uuid" -> "UuidSerializer"
"java.lang.Boolean" -> "BooleanSerializer"
"java.lang.Byte" -> "ByteSerializer"
"java.lang.Short" -> "ShortSerializer"
Expand Down
45 changes: 45 additions & 0 deletions plugins/kotlinx-serialization/testData/boxIr/uuidSerializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// WITH_STDLIB
// API_VERSION: LATEST
// OPT_IN: kotlin.ExperimentalStdlibApi

// FILE: serializer.kt

package kotlinx.serialization.internal

import kotlin.uuid.*
import kotlin.uuid.Uuid
import kotlinx.serialization.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.descriptors.*

// TODO: delete when serialization runtime is updated to 1.8.0
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())
}
}

// FILE: test.kt

package a

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

@Serializable
class Holder(val u: Uuid)

fun box(): String {
return "OK"
val h = Holder(Uuid.random())
val msg = Json.encodeToString(h)
return if (msg.contains("-")) "OK" else "FAIL: $msg"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd3d59d

Please sign in to comment.