-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic UuidSerializer until a new kotlinx-serialization release (#385
- Loading branch information
Showing
8 changed files
with
92 additions
and
9 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
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
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
25 changes: 25 additions & 0 deletions
25
kotlinx-uuid-core/src/commonMain/kotlin/app/softwork/uuid/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,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()) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
kotlinx-uuid-core/src/commonTest/kotlin/app/softwork/uuid/UuidSerializationTest.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,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) | ||
} | ||
} |
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
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
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