diff --git a/plugins/kotlinx-serialization/kotlinx-serialization.common/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt b/plugins/kotlinx-serialization/kotlinx-serialization.common/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt index 61b1863ede7ed..7c2eed27fa750 100644 --- a/plugins/kotlinx-serialization/kotlinx-serialization.common/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt +++ b/plugins/kotlinx-serialization/kotlinx-serialization.common/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt @@ -252,6 +252,7 @@ fun findStandardKotlinTypeSerializerName(typeName: String?): String? { "kotlin.DoubleArray" -> "DoubleArraySerializer" "kotlin.BooleanArray" -> "BooleanArraySerializer" "kotlin.time.Duration" -> "DurationSerializer" + "kotlin.time.Instant" -> "InstantSerializer" "kotlin.uuid.Uuid" -> "UuidSerializer" "java.lang.Boolean" -> "BooleanSerializer" "java.lang.Byte" -> "ByteSerializer" diff --git a/plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt b/plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt new file mode 100644 index 0000000000000..44c01dab6e15f --- /dev/null +++ b/plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt @@ -0,0 +1,43 @@ +// WITH_STDLIB +// API_VERSION: LATEST +// OPT_IN: kotlin.time.ExperimentalTime + +// FILE: serializer.kt + +package kotlinx.serialization.internal + +import kotlin.time.* +import kotlinx.serialization.* +import kotlinx.serialization.encoding.* +import kotlinx.serialization.descriptors.* + +// TODO: delete when serialization runtime is updated to 1.9.0 +internal object InstantSerializer : KSerializer { + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlin.time.Instant", PrimitiveKind.STRING) + + override fun serialize(encoder: Encoder, value: Instant) { + encoder.encodeString(value.toString()) + } + + override fun deserialize(decoder: Decoder): Instant { + return Instant.parse(decoder.decodeString()) + } +} + +// FILE: test.kt + +package a + +import kotlin.time.* +import kotlinx.serialization.* +import kotlinx.serialization.json.* + +@Serializable +class Holder(val i: Instant) + +fun box(): String { + val h = Holder(Instant.parse("2025-01-04T23:59:14.0001242Z")) + val msg = Json.encodeToString(h) + return if (msg == """{"i":"2025-01-04T23:59:14.000124200Z"}""") "OK" else "FAIL: $msg" +} + diff --git a/plugins/kotlinx-serialization/testData/boxIr/uuidSerializer.kt b/plugins/kotlinx-serialization/testData/boxIr/uuidSerializer.kt index da6f9cddb0748..f12b6c5aa06d5 100644 --- a/plugins/kotlinx-serialization/testData/boxIr/uuidSerializer.kt +++ b/plugins/kotlinx-serialization/testData/boxIr/uuidSerializer.kt @@ -38,9 +38,8 @@ import kotlinx.serialization.json.* 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" + return if (msg == """{"u":"bc501c76-d806-4578-b45e-97a264e280f1"}""") "OK" else "FAIL: $msg" } diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLReversedSerializationBlackBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLReversedSerializationBlackBoxTestGenerated.java index b0b08c6f7abac..10c74cdca9504 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLReversedSerializationBlackBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLReversedSerializationBlackBoxTestGenerated.java @@ -170,6 +170,12 @@ public void testInlineClasses() { runTest("plugins/kotlinx-serialization/testData/boxIr/inlineClasses.kt"); } + @Test + @TestMetadata("instantSerializer.kt") + public void testInstantSerializer() { + runTest("plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt"); + } + @Test @TestMetadata("interfaces.kt") public void testInterfaces() { diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLSerializationBlackBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLSerializationBlackBoxTestGenerated.java index cbe50df54226f..a978c6e248904 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLSerializationBlackBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/LLSerializationBlackBoxTestGenerated.java @@ -170,6 +170,12 @@ public void testInlineClasses() { runTest("plugins/kotlinx-serialization/testData/boxIr/inlineClasses.kt"); } + @Test + @TestMetadata("instantSerializer.kt") + public void testInstantSerializer() { + runTest("plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt"); + } + @Test @TestMetadata("interfaces.kt") public void testInterfaces() { diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirJsBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirJsBoxTestGenerated.java index c2baf48f32665..56a15da97e5aa 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirJsBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirJsBoxTestGenerated.java @@ -126,6 +126,12 @@ public void testGenerics() { runTest("plugins/kotlinx-serialization/testData/boxIr/generics.kt"); } + @Test + @TestMetadata("instantSerializer.kt") + public void testInstantSerializer() { + runTest("plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt"); + } + @Test @TestMetadata("interfaces.kt") public void testInterfaces() { diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java index 284ba272fc834..978f22df956c6 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java @@ -159,6 +159,12 @@ public void testInlineClasses() { runTest("plugins/kotlinx-serialization/testData/boxIr/inlineClasses.kt"); } + @Test + @TestMetadata("instantSerializer.kt") + public void testInstantSerializer() { + runTest("plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt"); + } + @Test @TestMetadata("interfaces.kt") public void testInterfaces() { diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java index 60b1b17461630..719ebe5a3730b 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java @@ -156,6 +156,12 @@ public void testInlineClasses() { runTest("plugins/kotlinx-serialization/testData/boxIr/inlineClasses.kt"); } + @Test + @TestMetadata("instantSerializer.kt") + public void testInstantSerializer() { + runTest("plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt"); + } + @Test @TestMetadata("interfaces.kt") public void testInterfaces() { diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrJsBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrJsBoxTestGenerated.java index f6c3a5082f865..ba7f686509c35 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrJsBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrJsBoxTestGenerated.java @@ -126,6 +126,12 @@ public void testGenerics() { runTest("plugins/kotlinx-serialization/testData/boxIr/generics.kt"); } + @Test + @TestMetadata("instantSerializer.kt") + public void testInstantSerializer() { + runTest("plugins/kotlinx-serialization/testData/boxIr/instantSerializer.kt"); + } + @Test @TestMetadata("interfaces.kt") public void testInterfaces() {