-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check against serialName instead of simpleClassName (#2802)
in PrimitiveSerialDescriptor constructor. By doing so, we allow users to declare their own serializers with short names like "Uuid" or "Duration", even though it is not recommended — because they may have been declared before library updates. Fixes #2799
- Loading branch information
1 parent
0b015e1
commit d9753af
Showing
3 changed files
with
64 additions
and
8 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
56 changes: 56 additions & 0 deletions
56
formats/json-tests/jvmTest/src/kotlinx/serialization/UuidPlatformClashTest.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,56 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.builtins.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.modules.* | ||
import kotlin.test.* | ||
import kotlin.uuid.* | ||
import java.util.UUID as JUuid | ||
import kotlin.uuid.Uuid as KUuid | ||
|
||
@OptIn(ExperimentalUuidApi::class) | ||
class UuidPlatformClashTest : JsonTestBase() { | ||
object JavaUuidSerializer : KSerializer<JUuid> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Uuid", PrimitiveKind.STRING) | ||
|
||
override fun serialize(encoder: Encoder, value: JUuid) { | ||
encoder.encodeString(value.toString().uppercase()) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): JUuid { | ||
return JUuid.fromString(decoder.decodeString()) | ||
} | ||
} | ||
|
||
|
||
@Serializable | ||
data class UuidPair( | ||
@Contextual val jUuid: JUuid, | ||
@Contextual val kUuid: KUuid, | ||
) | ||
|
||
@Test | ||
fun testUuids() { | ||
val module = SerializersModule { | ||
contextual(JavaUuidSerializer) | ||
contextual(KUuid.serializer()) | ||
} | ||
val json = Json { serializersModule = module } | ||
val pair = UuidPair( | ||
JUuid.fromString("252660b8-9a2b-44d1-a804-9a23f881cec5"), | ||
KUuid.parse("c8ad1526-4b7c-4b67-9f77-6d05e580ad71") | ||
) | ||
assertJsonFormAndRestored( | ||
UuidPair.serializer(), | ||
pair, | ||
"""{"jUuid":"252660B8-9A2B-44D1-A804-9A23F881CEC5","kUuid":"c8ad1526-4b7c-4b67-9f77-6d05e580ad71"}""", | ||
json | ||
) | ||
} | ||
} |