diff --git a/core/common/src/serializers/DateTimePeriodSerializers.kt b/core/common/src/serializers/DateTimePeriodSerializers.kt index eb5b0814a..3fd8e50fe 100644 --- a/core/common/src/serializers/DateTimePeriodSerializers.kt +++ b/core/common/src/serializers/DateTimePeriodSerializers.kt @@ -19,7 +19,7 @@ import kotlinx.serialization.encoding.* public object DateTimePeriodComponentSerializer: KSerializer { override val descriptor: SerialDescriptor = - buildClassSerialDescriptor("DateTimePeriod") { + buildClassSerialDescriptor("kotlinx.datetime.DateTimePeriod") { element("years", isOptional = true) element("months", isOptional = true) element("days", isOptional = true) @@ -81,7 +81,7 @@ public object DateTimePeriodComponentSerializer: KSerializer { public object DateTimePeriodIso8601Serializer: KSerializer { override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("DateTimePeriod", PrimitiveKind.STRING) + PrimitiveSerialDescriptor("kotlinx.datetime.DateTimePeriod", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): DateTimePeriod = DateTimePeriod.parse(decoder.decodeString()) @@ -110,7 +110,7 @@ public object DatePeriodComponentSerializer: KSerializer { private fun unexpectedNonzero(fieldName: String, value: Int) = unexpectedNonzero(fieldName, value.toLong()) override val descriptor: SerialDescriptor = - buildClassSerialDescriptor("DatePeriod") { + buildClassSerialDescriptor("kotlinx.datetime.DatePeriod") { element("years", isOptional = true) element("months", isOptional = true) element("days", isOptional = true) @@ -166,7 +166,7 @@ public object DatePeriodComponentSerializer: KSerializer { public object DatePeriodIso8601Serializer: KSerializer { override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("DatePeriod", PrimitiveKind.STRING) + PrimitiveSerialDescriptor("kotlinx.datetime.DatePeriod", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): DatePeriod = when (val period = DateTimePeriod.parse(decoder.decodeString())) { @@ -178,4 +178,4 @@ public object DatePeriodIso8601Serializer: KSerializer { encoder.encodeString(value.toString()) } -} \ No newline at end of file +} diff --git a/core/common/src/serializers/DateTimeUnitSerializers.kt b/core/common/src/serializers/DateTimeUnitSerializers.kt index f003d9bfc..db2351e61 100644 --- a/core/common/src/serializers/DateTimeUnitSerializers.kt +++ b/core/common/src/serializers/DateTimeUnitSerializers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 JetBrains s.r.o. + * Copyright 2019-2023 JetBrains s.r.o. * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. */ @@ -23,7 +23,7 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer("nanoseconds") } } @@ -35,7 +35,6 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer break@loop // https://youtrack.jetbrains.com/issue/KT-42262 - else -> throw UnknownFieldException(elementIndex) + else -> throwUnknownIndexException(elementIndex) } } } } - if (!seen) throw MissingFieldException("nanoseconds") + if (!seen) throw MissingFieldException(missingField = "nanoseconds", serialName = descriptor.serialName) return DateTimeUnit.TimeBased(nanoseconds) } } @@ -70,7 +69,7 @@ public object DayBasedDateTimeUnitSerializer: KSerializer // https://youtrack.jetbrains.com/issue/KT-63939 override val descriptor: SerialDescriptor by lazy(LazyThreadSafetyMode.PUBLICATION) { - buildClassSerialDescriptor("DayBased") { + buildClassSerialDescriptor("kotlinx.datetime.DayBased") { element("days") } } @@ -82,7 +81,6 @@ public object DayBasedDateTimeUnitSerializer: KSerializer } @OptIn(ExperimentalSerializationApi::class) - @Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException` override fun deserialize(decoder: Decoder): DateTimeUnit.DayBased { var seen = false var days = 0 @@ -98,12 +96,12 @@ public object DayBasedDateTimeUnitSerializer: KSerializer seen = true } CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262 - else -> throw UnknownFieldException(elementIndex) + else -> throwUnknownIndexException(elementIndex) } } } } - if (!seen) throw MissingFieldException("days") + if (!seen) throw MissingFieldException(missingField = "days", serialName = descriptor.serialName) return DateTimeUnit.DayBased(days) } } @@ -117,7 +115,7 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer("months") } } @@ -129,7 +127,6 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer break@loop // https://youtrack.jetbrains.com/issue/KT-42262 - else -> throw UnknownFieldException(elementIndex) + else -> throwUnknownIndexException(elementIndex) } } } } - if (!seen) throw MissingFieldException("months") + if (!seen) throw MissingFieldException(missingField = "months", serialName = descriptor.serialName) return DateTimeUnit.MonthBased(months) } } @@ -160,7 +157,7 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer() { @@ -197,9 +194,9 @@ public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer() { +public object DateTimeUnitSerializer : AbstractPolymorphicSerializer() { // https://youtrack.jetbrains.com/issue/KT-63939 private val impl by lazy(LazyThreadSafetyMode.PUBLICATION) { @@ -224,4 +221,8 @@ public object DateTimeUnitSerializer: AbstractPolymorphicSerializer { - private val impl = EnumSerializer("Month", DayOfWeek.values()) - - override val descriptor: SerialDescriptor - get() = impl.descriptor - - override fun deserialize(decoder: Decoder): DayOfWeek = impl.deserialize(decoder) - - override fun serialize(encoder: Encoder, value: DayOfWeek): Unit = impl.serialize(encoder, value) -} +@Suppress("EnumValuesSoftDeprecate") // createEnumSerializer requires an array +public object DayOfWeekSerializer : KSerializer by createEnumSerializer( + "kotlinx.datetime.DayOfWeek", + DayOfWeek.values() +) diff --git a/core/common/src/serializers/InstantSerializers.kt b/core/common/src/serializers/InstantSerializers.kt index d2cb3df49..6c0c07678 100644 --- a/core/common/src/serializers/InstantSerializers.kt +++ b/core/common/src/serializers/InstantSerializers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 JetBrains s.r.o. + * Copyright 2019-2023 JetBrains s.r.o. * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. */ @@ -18,10 +18,10 @@ import kotlinx.serialization.encoding.* * @see Instant.toString * @see Instant.parse */ -public object InstantIso8601Serializer: KSerializer { +public object InstantIso8601Serializer : KSerializer { override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("Instant", PrimitiveKind.STRING) + PrimitiveSerialDescriptor("kotlinx.datetime.Instant", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): Instant = Instant.parse(decoder.decodeString()) @@ -37,21 +37,20 @@ public object InstantIso8601Serializer: KSerializer { * * JSON example: `{"epochSeconds":1607505416,"nanosecondsOfSecond":124000}` */ -public object InstantComponentSerializer: KSerializer { +public object InstantComponentSerializer : KSerializer { override val descriptor: SerialDescriptor = - buildClassSerialDescriptor("Instant") { + buildClassSerialDescriptor("kotlinx.datetime.Instant") { element("epochSeconds") element("nanosecondsOfSecond", isOptional = true) } @OptIn(ExperimentalSerializationApi::class) - @Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException` override fun deserialize(decoder: Decoder): Instant = decoder.decodeStructure(descriptor) { var epochSeconds: Long? = null var nanosecondsOfSecond = 0 - loop@while (true) { + loop@ while (true) { when (val index = decodeElementIndex(descriptor)) { 0 -> epochSeconds = decodeLongElement(descriptor, 0) 1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1) @@ -59,7 +58,10 @@ public object InstantComponentSerializer: KSerializer { else -> throw SerializationException("Unexpected index: $index") } } - if (epochSeconds == null) throw MissingFieldException("epochSeconds") + if (epochSeconds == null) throw MissingFieldException( + missingField = "epochSeconds", + serialName = descriptor.serialName + ) Instant.fromEpochSeconds(epochSeconds, nanosecondsOfSecond) } diff --git a/core/common/src/serializers/LocalDateSerializers.kt b/core/common/src/serializers/LocalDateSerializers.kt index 5f2770507..6d9f328ff 100644 --- a/core/common/src/serializers/LocalDateSerializers.kt +++ b/core/common/src/serializers/LocalDateSerializers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 JetBrains s.r.o. + * Copyright 2019-2023 JetBrains s.r.o. * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. */ @@ -21,7 +21,7 @@ import kotlinx.serialization.encoding.* public object LocalDateIso8601Serializer: KSerializer { override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING) + PrimitiveSerialDescriptor("kotlinx.datetime.LocalDate", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): LocalDate = LocalDate.parse(decoder.decodeString()) @@ -40,14 +40,13 @@ public object LocalDateIso8601Serializer: KSerializer { public object LocalDateComponentSerializer: KSerializer { override val descriptor: SerialDescriptor = - buildClassSerialDescriptor("LocalDate") { + buildClassSerialDescriptor("kotlinx.datetime.LocalDate") { element("year") element("month") element("day") } @OptIn(ExperimentalSerializationApi::class) - @Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException` override fun deserialize(decoder: Decoder): LocalDate = decoder.decodeStructure(descriptor) { var year: Int? = null @@ -59,12 +58,12 @@ public object LocalDateComponentSerializer: KSerializer { 1 -> month = decodeShortElement(descriptor, 1) 2 -> day = decodeShortElement(descriptor, 2) CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262 - else -> throw SerializationException("Unexpected index: $index") + else -> throwUnknownIndexException(index) } } - if (year == null) throw MissingFieldException("year") - if (month == null) throw MissingFieldException("month") - if (day == null) throw MissingFieldException("day") + if (year == null) throw MissingFieldException(missingField = "year", serialName = descriptor.serialName) + if (month == null) throw MissingFieldException(missingField = "month", serialName = descriptor.serialName) + if (day == null) throw MissingFieldException(missingField = "day", serialName = descriptor.serialName) LocalDate(year, month.toInt(), day.toInt()) } diff --git a/core/common/src/serializers/LocalDateTimeSerializers.kt b/core/common/src/serializers/LocalDateTimeSerializers.kt index 87d610630..d22d2243d 100644 --- a/core/common/src/serializers/LocalDateTimeSerializers.kt +++ b/core/common/src/serializers/LocalDateTimeSerializers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 JetBrains s.r.o. + * Copyright 2019-2023 JetBrains s.r.o. * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. */ @@ -21,7 +21,7 @@ import kotlinx.serialization.encoding.* public object LocalDateTimeIso8601Serializer: KSerializer { override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING) + PrimitiveSerialDescriptor("kotlinx.datetime.LocalDateTime", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): LocalDateTime = LocalDateTime.parse(decoder.decodeString()) @@ -40,7 +40,7 @@ public object LocalDateTimeIso8601Serializer: KSerializer { public object LocalDateTimeComponentSerializer: KSerializer { override val descriptor: SerialDescriptor = - buildClassSerialDescriptor("LocalDateTime") { + buildClassSerialDescriptor("kotlinx.datetime.LocalDateTime") { element("year") element("month") element("day") @@ -51,7 +51,6 @@ public object LocalDateTimeComponentSerializer: KSerializer { } @OptIn(ExperimentalSerializationApi::class) - @Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException` override fun deserialize(decoder: Decoder): LocalDateTime = decoder.decodeStructure(descriptor) { var year: Int? = null @@ -74,11 +73,11 @@ public object LocalDateTimeComponentSerializer: KSerializer { else -> throw SerializationException("Unexpected index: $index") } } - if (year == null) throw MissingFieldException("year") - if (month == null) throw MissingFieldException("month") - if (day == null) throw MissingFieldException("day") - if (hour == null) throw MissingFieldException("hour") - if (minute == null) throw MissingFieldException("minute") + if (year == null) throw MissingFieldException(missingField = "year", serialName = descriptor.serialName) + if (month == null) throw MissingFieldException(missingField = "month", serialName = descriptor.serialName) + if (day == null) throw MissingFieldException(missingField = "day", serialName = descriptor.serialName) + if (hour == null) throw MissingFieldException(missingField = "hour", serialName = descriptor.serialName) + if (minute == null) throw MissingFieldException(missingField = "minute", serialName = descriptor.serialName) LocalDateTime(year, month.toInt(), day.toInt(), hour.toInt(), minute.toInt(), second.toInt(), nanosecond) } diff --git a/core/common/src/serializers/LocalTimeSerializers.kt b/core/common/src/serializers/LocalTimeSerializers.kt index 68362c29d..0becdc93a 100644 --- a/core/common/src/serializers/LocalTimeSerializers.kt +++ b/core/common/src/serializers/LocalTimeSerializers.kt @@ -21,7 +21,7 @@ import kotlinx.serialization.encoding.* public object LocalTimeIso8601Serializer : KSerializer { override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("LocalTime", PrimitiveKind.STRING) + PrimitiveSerialDescriptor("kotlinx.datetime.LocalTime", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): LocalTime = LocalTime.parse(decoder.decodeString()) @@ -39,7 +39,7 @@ public object LocalTimeIso8601Serializer : KSerializer { public object LocalTimeComponentSerializer : KSerializer { override val descriptor: SerialDescriptor = - buildClassSerialDescriptor("LocalTime") { + buildClassSerialDescriptor("kotlinx.datetime.LocalTime") { element("hour") element("minute") element("second", isOptional = true) @@ -47,7 +47,6 @@ public object LocalTimeComponentSerializer : KSerializer { } @OptIn(ExperimentalSerializationApi::class) - @Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException` override fun deserialize(decoder: Decoder): LocalTime = decoder.decodeStructure(descriptor) { var hour: Short? = null @@ -64,8 +63,8 @@ public object LocalTimeComponentSerializer : KSerializer { else -> throw SerializationException("Unexpected index: $index") } } - if (hour == null) throw MissingFieldException("hour") - if (minute == null) throw MissingFieldException("minute") + if (hour == null) throw MissingFieldException(missingField = "hour", serialName = descriptor.serialName) + if (minute == null) throw MissingFieldException(missingField = "minute", serialName = descriptor.serialName) LocalTime(hour.toInt(), minute.toInt(), second.toInt(), nanosecond) } diff --git a/core/common/src/serializers/MonthSerializers.kt b/core/common/src/serializers/MonthSerializers.kt index aea235cb1..74d106549 100644 --- a/core/common/src/serializers/MonthSerializers.kt +++ b/core/common/src/serializers/MonthSerializers.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 JetBrains s.r.o. + * Copyright 2019-2023 JetBrains s.r.o. * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. */ @@ -16,14 +16,13 @@ import kotlinx.serialization.internal.* * * JSON example: `"JANUARY"` */ -@Suppress("INVISIBLE_MEMBER") -public object MonthSerializer: KSerializer { - private val impl = EnumSerializer("Month", Month.values()) +@Suppress("EnumValuesSoftDeprecate") // createEnumSerializer requires an array +public object MonthSerializer : KSerializer by createEnumSerializer( + "kotlinx.datetime.Month", + Month.values()) - override val descriptor: SerialDescriptor - get() = impl.descriptor - - override fun deserialize(decoder: Decoder): Month = impl.deserialize(decoder) - - override fun serialize(encoder: Encoder, value: Month): Unit = impl.serialize(encoder, value) +// Until https://github.com/Kotlin/kotlinx.serialization/issues/2459 is resolved +internal fun > createEnumSerializer(serialName: String, values: Array): KSerializer { + @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") + return kotlinx.serialization.internal.EnumSerializer(serialName, values) } diff --git a/core/common/src/serializers/TimeZoneSerializers.kt b/core/common/src/serializers/TimeZoneSerializers.kt index a4961ea34..ad41ba1b4 100644 --- a/core/common/src/serializers/TimeZoneSerializers.kt +++ b/core/common/src/serializers/TimeZoneSerializers.kt @@ -19,7 +19,7 @@ import kotlinx.serialization.encoding.* */ public object TimeZoneSerializer: KSerializer { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("TimeZone", PrimitiveKind.STRING) + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.TimeZone", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): TimeZone = TimeZone.of(decoder.decodeString()) @@ -36,7 +36,7 @@ public object TimeZoneSerializer: KSerializer { */ public object FixedOffsetTimeZoneSerializer: KSerializer { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("FixedOffsetTimeZone", PrimitiveKind.STRING) + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.FixedOffsetTimeZone", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): FixedOffsetTimeZone { val zone = TimeZone.of(decoder.decodeString()) @@ -63,7 +63,7 @@ public object FixedOffsetTimeZoneSerializer: KSerializer { */ public object UtcOffsetSerializer: KSerializer { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("UtcOffset", PrimitiveKind.STRING) + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.UtcOffset", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): UtcOffset { return UtcOffset.parse(decoder.decodeString()) diff --git a/serialization/common/test/DateTimeUnitSerializationTest.kt b/serialization/common/test/DateTimeUnitSerializationTest.kt index b221fa5b7..e2b4f8b6e 100644 --- a/serialization/common/test/DateTimeUnitSerializationTest.kt +++ b/serialization/common/test/DateTimeUnitSerializationTest.kt @@ -48,14 +48,14 @@ class DateTimeUnitSerializationTest { repeat(100) { val days = Random.nextInt(1, Int.MAX_VALUE) val unit = DateTimeUnit.DayBased(days) - val json = "{\"type\":\"DayBased\",\"days\":$days}" + val json = "{\"type\":\"kotlinx.datetime.DayBased\",\"days\":$days}" assertEquals(json, Json.encodeToString(serializer, unit)) assertEquals(unit, Json.decodeFromString(serializer, json)) } repeat(100) { val months = Random.nextInt(1, Int.MAX_VALUE) val unit = DateTimeUnit.MonthBased(months) - val json = "{\"type\":\"MonthBased\",\"months\":$months}" + val json = "{\"type\":\"kotlinx.datetime.MonthBased\",\"months\":$months}" assertEquals(json, Json.encodeToString(serializer, unit)) assertEquals(unit, Json.decodeFromString(serializer, json)) } @@ -65,21 +65,21 @@ class DateTimeUnitSerializationTest { repeat(100) { val nanoseconds = Random.nextLong(1, Long.MAX_VALUE) val unit = DateTimeUnit.TimeBased(nanoseconds) - val json = "{\"type\":\"TimeBased\",\"nanoseconds\":${nanoseconds.toString()}}" // https://youtrack.jetbrains.com/issue/KT-39891 + val json = "{\"type\":\"kotlinx.datetime.TimeBased\",\"nanoseconds\":${nanoseconds.toString()}}" // https://youtrack.jetbrains.com/issue/KT-39891 assertEquals(json, Json.encodeToString(serializer, unit)) assertEquals(unit, Json.decodeFromString(serializer, json)) } repeat(100) { val days = Random.nextInt(1, Int.MAX_VALUE) val unit = DateTimeUnit.DayBased(days) - val json = "{\"type\":\"DayBased\",\"days\":$days}" + val json = "{\"type\":\"kotlinx.datetime.DayBased\",\"days\":$days}" assertEquals(json, Json.encodeToString(serializer, unit)) assertEquals(unit, Json.decodeFromString(serializer, json)) } repeat(100) { val months = Random.nextInt(1, Int.MAX_VALUE) val unit = DateTimeUnit.MonthBased(months) - val json = "{\"type\":\"MonthBased\",\"months\":$months}" + val json = "{\"type\":\"kotlinx.datetime.MonthBased\",\"months\":$months}" assertEquals(json, Json.encodeToString(serializer, unit)) assertEquals(unit, Json.decodeFromString(serializer, json)) } @@ -119,4 +119,4 @@ class DateTimeUnitSerializationTest { serialization(Json.serializersModule.serializer()) } -} \ No newline at end of file +}