-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UuidSerializer as a standard serializer to kotlinx-serialization …
…plugin UuidSerializer should appear in 1.7.1. Related ticket: Kotlin/kotlinx.serialization#2730
- Loading branch information
1 parent
5d704b2
commit b37a447
Showing
6 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
plugins/kotlinx-serialization/testData/boxIr/uuidSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// WITH_STDLIB | ||
// API_VERSION: LATEST | ||
// OPT_IN: kotlin.ExperimentalStdlibApi | ||
// OPT_IN: kotlin.uuid.ExperimentalUuidApi | ||
|
||
// 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.parse("bc501c76-d806-4578-b45e-97a264e280f1")) | ||
val msg = Json.encodeToString(h) | ||
return if (msg == """{"uuid":"bc501c76-d806-4578-b45e-97a264e280f1"}""") "OK" else "FAIL: $msg" | ||
} | ||
|
6 changes: 6 additions & 0 deletions
6
...s-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirJsBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...tbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ts-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrJsBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.