diff --git a/core/common/src/Instant.kt b/core/common/src/Instant.kt index 6168b7b8..f9f227c3 100644 --- a/core/common/src/Instant.kt +++ b/core/common/src/Instant.kt @@ -7,8 +7,7 @@ package kotlinx.datetime import kotlinx.datetime.format.* import kotlinx.datetime.internal.* -import kotlinx.datetime.serializers.InstantIso8601Serializer -import kotlinx.datetime.serializers.InstantComponentSerializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlin.time.* @@ -194,7 +193,7 @@ import kotlin.time.* * * @see LocalDateTime for a user-visible representation of moments in time in an unspecified time zone. */ -@Serializable(with = InstantIso8601Serializer::class) +@Serializable(with = InstantSerializer::class) public expect class Instant : Comparable { /** diff --git a/core/common/src/LocalDate.kt b/core/common/src/LocalDate.kt index 0146d6f9..aa1ad943 100644 --- a/core/common/src/LocalDate.kt +++ b/core/common/src/LocalDate.kt @@ -64,7 +64,7 @@ import kotlinx.serialization.Serializable * @sample kotlinx.datetime.test.samples.LocalDateSamples.simpleParsingAndFormatting * @sample kotlinx.datetime.test.samples.LocalDateSamples.customFormat */ -@Serializable(with = LocalDateIso8601Serializer::class) +@Serializable(with = LocalDateSerializer::class) public expect class LocalDate : Comparable { public companion object { /** diff --git a/core/common/src/LocalDateTime.kt b/core/common/src/LocalDateTime.kt index dad1a2c2..e386217a 100644 --- a/core/common/src/LocalDateTime.kt +++ b/core/common/src/LocalDateTime.kt @@ -6,8 +6,7 @@ package kotlinx.datetime import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer -import kotlinx.datetime.serializers.LocalDateTimeComponentSerializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable /** @@ -105,7 +104,7 @@ import kotlinx.serialization.Serializable * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.simpleParsingAndFormatting * @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.customFormat */ -@Serializable(with = LocalDateTimeIso8601Serializer::class) +@Serializable(with = LocalDateTimeSerializer::class) public expect class LocalDateTime : Comparable { public companion object { diff --git a/core/common/src/LocalTime.kt b/core/common/src/LocalTime.kt index 79c61793..d842b6ce 100644 --- a/core/common/src/LocalTime.kt +++ b/core/common/src/LocalTime.kt @@ -7,8 +7,7 @@ package kotlinx.datetime import kotlinx.datetime.LocalDate.Companion.parse import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.LocalTimeIso8601Serializer -import kotlinx.datetime.serializers.LocalTimeComponentSerializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable @@ -76,7 +75,7 @@ import kotlinx.serialization.Serializable * @sample kotlinx.datetime.test.samples.LocalTimeSamples.simpleParsingAndFormatting * @sample kotlinx.datetime.test.samples.LocalTimeSamples.customFormat */ -@Serializable(LocalTimeIso8601Serializer::class) +@Serializable(LocalTimeSerializer::class) public expect class LocalTime : Comparable { public companion object { diff --git a/core/common/src/UtcOffset.kt b/core/common/src/UtcOffset.kt index 5c5bd7fd..63df9bf0 100644 --- a/core/common/src/UtcOffset.kt +++ b/core/common/src/UtcOffset.kt @@ -6,7 +6,7 @@ package kotlinx.datetime import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.UtcOffsetSerializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable /** @@ -53,6 +53,7 @@ import kotlinx.serialization.Serializable * @sample kotlinx.datetime.test.samples.UtcOffsetSamples.simpleParsingAndFormatting * @sample kotlinx.datetime.test.samples.UtcOffsetSamples.customFormat */ +@Suppress("DEPRECATION") @Serializable(with = UtcOffsetSerializer::class) public expect class UtcOffset { /** diff --git a/core/common/src/serializers/InstantSerializers.kt b/core/common/src/serializers/InstantSerializers.kt index c64bdf47..18d972fb 100644 --- a/core/common/src/serializers/InstantSerializers.kt +++ b/core/common/src/serializers/InstantSerializers.kt @@ -6,6 +6,8 @@ package kotlinx.datetime.serializers import kotlinx.datetime.Instant +import kotlinx.datetime.format +import kotlinx.datetime.format.DateTimeComponents import kotlinx.serialization.* import kotlinx.serialization.descriptors.* import kotlinx.serialization.encoding.* @@ -15,8 +17,7 @@ import kotlinx.serialization.encoding.* * * JSON example: `"2020-12-09T09:16:56.000124Z"` * - * @see Instant.toString - * @see Instant.parse + * @see DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET */ public object InstantIso8601Serializer : KSerializer { @@ -24,10 +25,10 @@ public object InstantIso8601Serializer : KSerializer { PrimitiveSerialDescriptor("kotlinx.datetime.Instant", PrimitiveKind.STRING) override fun deserialize(decoder: Decoder): Instant = - Instant.parse(decoder.decodeString()) + Instant.parse(decoder.decodeString(), DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET) override fun serialize(encoder: Encoder, value: Instant) { - encoder.encodeString(value.toString()) + encoder.encodeString(value.format(DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET)) } } @@ -75,3 +76,22 @@ public object InstantComponentSerializer : KSerializer { } } + +/** + * A serializer for [Instant] that uses the default [Instant.toString]/[Instant.parse]. + * + * JSON example: `"2020-12-09T09:16:56.000124Z"` + */ +@PublishedApi internal object InstantSerializer : KSerializer { + + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor("kotlinx.datetime.Instant", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): Instant = + Instant.parse(decoder.decodeString()) + + override fun serialize(encoder: Encoder, value: Instant) { + encoder.encodeString(value.toString()) + } + +} diff --git a/core/common/src/serializers/LocalDateSerializers.kt b/core/common/src/serializers/LocalDateSerializers.kt index e1c1c5e9..3b5e52dd 100644 --- a/core/common/src/serializers/LocalDateSerializers.kt +++ b/core/common/src/serializers/LocalDateSerializers.kt @@ -6,6 +6,7 @@ package kotlinx.datetime.serializers import kotlinx.datetime.LocalDate +import kotlinx.datetime.format.DateTimeFormat import kotlinx.serialization.* import kotlinx.serialization.descriptors.* import kotlinx.serialization.encoding.* @@ -15,22 +16,10 @@ import kotlinx.serialization.encoding.* * * JSON example: `"2020-01-01"` * - * @see LocalDate.parse - * @see LocalDate.toString + * @see LocalDate.Formats.ISO */ -public object LocalDateIso8601Serializer: KSerializer { - - override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("kotlinx.datetime.LocalDate", PrimitiveKind.STRING) - - override fun deserialize(decoder: Decoder): LocalDate = - LocalDate.parse(decoder.decodeString()) - - override fun serialize(encoder: Encoder, value: LocalDate) { - encoder.encodeString(value.toString()) - } - -} +public object LocalDateIso8601Serializer : KSerializer +by DateTimeFormatSerializer(LocalDate.Formats.ISO, "kotlinx.datetime.LocalDate") /** * A serializer for [LocalDate] that represents a value as its components. @@ -76,3 +65,38 @@ public object LocalDateComponentSerializer: KSerializer { } } + +/** + * A serializer for [LocalDate] that uses the default [LocalDate.toString]/[LocalDate.parse]. + * + * JSON example: `"2020-01-01"` + */ +@PublishedApi internal object LocalDateSerializer: KSerializer { + + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor("kotlinx.datetime.LocalDate", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): LocalDate = + LocalDate.parse(decoder.decodeString()) + + override fun serialize(encoder: Encoder, value: LocalDate) { + encoder.encodeString(value.toString()) + } + +} + +/** + * A general mechanism of implementing [KSerializer] instances using the given [string formats][DateTimeFormat]. + */ +internal class DateTimeFormatSerializer(val format: DateTimeFormat, className: String): KSerializer { + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor(className, PrimitiveKind.STRING) + + override fun serialize(encoder: Encoder, value: T) { + encoder.encodeString(format.format(value)) + } + + override fun deserialize(decoder: Decoder): T = + format.parse(decoder.decodeString()) + +} diff --git a/core/common/src/serializers/LocalDateTimeSerializers.kt b/core/common/src/serializers/LocalDateTimeSerializers.kt index 73f29115..3912fe7f 100644 --- a/core/common/src/serializers/LocalDateTimeSerializers.kt +++ b/core/common/src/serializers/LocalDateTimeSerializers.kt @@ -15,22 +15,10 @@ import kotlinx.serialization.encoding.* * * JSON example: `"2007-12-31T23:59:01"` * - * @see LocalDateTime.parse - * @see LocalDateTime.toString + * @see LocalDateTime.Formats.ISO */ -public object LocalDateTimeIso8601Serializer: KSerializer { - - override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("kotlinx.datetime.LocalDateTime", PrimitiveKind.STRING) - - override fun deserialize(decoder: Decoder): LocalDateTime = - LocalDateTime.parse(decoder.decodeString()) - - override fun serialize(encoder: Encoder, value: LocalDateTime) { - encoder.encodeString(value.toString()) - } - -} +public object LocalDateTimeIso8601Serializer : KSerializer +by DateTimeFormatSerializer(LocalDateTime.Formats.ISO, "kotlinx.datetime.LocalDateTime") /** * A serializer for [LocalDateTime] that represents a value as its components. @@ -98,3 +86,22 @@ public object LocalDateTimeComponentSerializer: KSerializer { } } + +/** + * A serializer for [LocalDateTime] that uses the default [LocalDateTime.toString]/[LocalDateTime.parse]. + * + * JSON example: `"2007-12-31T23:59:01"` + */ +public object LocalDateTimeSerializer: KSerializer { + + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor("kotlinx.datetime.LocalDateTime", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): LocalDateTime = + LocalDateTime.parse(decoder.decodeString()) + + override fun serialize(encoder: Encoder, value: LocalDateTime) { + encoder.encodeString(value.toString()) + } + +} diff --git a/core/common/src/serializers/LocalTimeSerializers.kt b/core/common/src/serializers/LocalTimeSerializers.kt index b8c1c0eb..054953cc 100644 --- a/core/common/src/serializers/LocalTimeSerializers.kt +++ b/core/common/src/serializers/LocalTimeSerializers.kt @@ -15,21 +15,10 @@ import kotlinx.serialization.encoding.* * * JSON example: `"12:01:03.999"` * - * @see LocalDate.parse - * @see LocalDate.toString + * @see LocalTime.Formats.ISO */ -public object LocalTimeIso8601Serializer : KSerializer { - - override val descriptor: SerialDescriptor = - PrimitiveSerialDescriptor("kotlinx.datetime.LocalTime", PrimitiveKind.STRING) - - override fun deserialize(decoder: Decoder): LocalTime = - LocalTime.parse(decoder.decodeString()) - - override fun serialize(encoder: Encoder, value: LocalTime) { - encoder.encodeString(value.toString()) - } -} +public object LocalTimeIso8601Serializer : KSerializer +by DateTimeFormatSerializer(LocalTime.Formats.ISO, "kotlinx.datetime.LocalTime") /** * A serializer for [LocalTime] that represents a value as its components. @@ -81,3 +70,24 @@ public object LocalTimeComponentSerializer : KSerializer { } } } + +/** + * A serializer for [LocalTime] that uses the ISO 8601 representation. + * + * JSON example: `"12:01:03.999"` + * + * @see LocalDate.parse + * @see LocalDate.toString + */ +public object LocalTimeSerializer : KSerializer { + + override val descriptor: SerialDescriptor = + PrimitiveSerialDescriptor("kotlinx.datetime.LocalTime", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): LocalTime = + LocalTime.parse(decoder.decodeString()) + + override fun serialize(encoder: Encoder, value: LocalTime) { + encoder.encodeString(value.toString()) + } +} diff --git a/core/common/src/serializers/TimeZoneSerializers.kt b/core/common/src/serializers/TimeZoneSerializers.kt index 2d6d1c38..bc8fa4b1 100644 --- a/core/common/src/serializers/TimeZoneSerializers.kt +++ b/core/common/src/serializers/TimeZoneSerializers.kt @@ -5,9 +5,7 @@ package kotlinx.datetime.serializers -import kotlinx.datetime.FixedOffsetTimeZone -import kotlinx.datetime.TimeZone -import kotlinx.datetime.UtcOffset +import kotlinx.datetime.* import kotlinx.serialization.* import kotlinx.serialization.descriptors.* import kotlinx.serialization.encoding.* @@ -54,13 +52,21 @@ public object FixedOffsetTimeZoneSerializer: KSerializer { } /** - * A serializer for [UtcOffset] that uses the extended ISO 8601 representation. + * A serializer for [UtcOffset] that uses the ISO 8601 representation. * * JSON example: `"+02:00"` * - * @see UtcOffset.parse - * @see UtcOffset.toString + * @see UtcOffset.Formats.ISO */ +public object UtcOffsetIso8601Serializer : KSerializer +by DateTimeFormatSerializer(UtcOffset.Formats.ISO, "kotlinx.datetime.UtcOffset") + +/** + * A serializer for [UtcOffset] that uses the default [UtcOffset.toString]/[UtcOffset.parse]. + * + * JSON example: `"+02:00"` + */ +@Deprecated("Use UtcOffset.serializer() instead", ReplaceWith("UtcOffset.serializer()")) public object UtcOffsetSerializer: KSerializer { override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.UtcOffset", PrimitiveKind.STRING) diff --git a/core/commonJs/src/Instant.kt b/core/commonJs/src/Instant.kt index af7cc868..45c6c2f8 100644 --- a/core/commonJs/src/Instant.kt +++ b/core/commonJs/src/Instant.kt @@ -13,13 +13,13 @@ import kotlinx.datetime.internal.JSJoda.ChronoUnit as jtChronoUnit import kotlinx.datetime.internal.JSJoda.ZonedDateTime as jtZonedDateTime import kotlinx.datetime.internal.safeAdd import kotlinx.datetime.internal.* -import kotlinx.datetime.serializers.InstantIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlin.time.* import kotlin.time.Duration.Companion.nanoseconds import kotlin.time.Duration.Companion.seconds -@Serializable(with = InstantIso8601Serializer::class) +@Serializable(with = InstantSerializer::class) public actual class Instant internal constructor(internal val value: jtInstant) : Comparable { public actual val epochSeconds: Long diff --git a/core/commonJs/src/LocalDate.kt b/core/commonJs/src/LocalDate.kt index a650665d..2f6285cb 100644 --- a/core/commonJs/src/LocalDate.kt +++ b/core/commonJs/src/LocalDate.kt @@ -6,12 +6,12 @@ package kotlinx.datetime import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.LocalDateIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlinx.datetime.internal.JSJoda.LocalDate as jtLocalDate import kotlinx.datetime.internal.JSJoda.ChronoUnit as jtChronoUnit -@Serializable(with = LocalDateIso8601Serializer::class) +@Serializable(with = LocalDateSerializer::class) public actual class LocalDate internal constructor(internal val value: jtLocalDate) : Comparable { public actual companion object { diff --git a/core/commonJs/src/LocalDateTime.kt b/core/commonJs/src/LocalDateTime.kt index c85e772a..59dfaaab 100644 --- a/core/commonJs/src/LocalDateTime.kt +++ b/core/commonJs/src/LocalDateTime.kt @@ -7,11 +7,11 @@ package kotlinx.datetime import kotlinx.datetime.format.* import kotlinx.datetime.format.ISO_DATETIME import kotlinx.datetime.format.LocalDateTimeFormat -import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlinx.datetime.internal.JSJoda.LocalDateTime as jtLocalDateTime -@Serializable(with = LocalDateTimeIso8601Serializer::class) +@Serializable(with = LocalDateTimeSerializer::class) public actual class LocalDateTime internal constructor(internal val value: jtLocalDateTime) : Comparable { public actual constructor(year: Int, monthNumber: Int, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) : diff --git a/core/commonJs/src/LocalTime.kt b/core/commonJs/src/LocalTime.kt index 674e5cb6..72aab223 100644 --- a/core/commonJs/src/LocalTime.kt +++ b/core/commonJs/src/LocalTime.kt @@ -8,11 +8,11 @@ import kotlinx.datetime.format.* import kotlinx.datetime.format.ISO_TIME import kotlinx.datetime.format.LocalTimeFormat import kotlinx.datetime.internal.* -import kotlinx.datetime.serializers.LocalTimeIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlinx.datetime.internal.JSJoda.LocalTime as jtLocalTime -@Serializable(LocalTimeIso8601Serializer::class) +@Serializable(LocalTimeSerializer::class) public actual class LocalTime internal constructor(internal val value: jtLocalTime) : Comparable { diff --git a/core/commonJs/src/UtcOffset.kt b/core/commonJs/src/UtcOffset.kt index 65dcf865..8f0ed74e 100644 --- a/core/commonJs/src/UtcOffset.kt +++ b/core/commonJs/src/UtcOffset.kt @@ -11,9 +11,10 @@ import kotlinx.datetime.internal.JSJoda.DateTimeFormatterBuilder as jtDateTimeFo import kotlinx.datetime.internal.JSJoda.DateTimeFormatter as jtDateTimeFormatter import kotlinx.datetime.internal.JSJoda.ResolverStyle as jtResolverStyle import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.UtcOffsetSerializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable +@Suppress("DEPRECATION") @Serializable(with = UtcOffsetSerializer::class) public actual class UtcOffset internal constructor(internal val zoneOffset: jtZoneOffset) { public actual val totalSeconds: Int get() = zoneOffset.totalSeconds() diff --git a/core/jvm/src/Instant.kt b/core/jvm/src/Instant.kt index ab347464..b7783206 100644 --- a/core/jvm/src/Instant.kt +++ b/core/jvm/src/Instant.kt @@ -9,7 +9,7 @@ package kotlinx.datetime import kotlinx.datetime.format.* import kotlinx.datetime.internal.safeMultiply import kotlinx.datetime.internal.* -import kotlinx.datetime.serializers.InstantIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import java.time.DateTimeException import java.time.temporal.* @@ -19,7 +19,7 @@ import kotlin.time.Duration.Companion.seconds import java.time.Instant as jtInstant import java.time.Clock as jtClock -@Serializable(with = InstantIso8601Serializer::class) +@Serializable(with = InstantSerializer::class) public actual class Instant internal constructor(internal val value: jtInstant) : Comparable { public actual val epochSeconds: Long diff --git a/core/jvm/src/LocalDate.kt b/core/jvm/src/LocalDate.kt index fe3b9ae1..c0d2be84 100644 --- a/core/jvm/src/LocalDate.kt +++ b/core/jvm/src/LocalDate.kt @@ -9,14 +9,14 @@ import kotlinx.datetime.format.* import kotlinx.datetime.internal.safeAdd import kotlinx.datetime.internal.safeMultiply import kotlinx.datetime.internal.* -import kotlinx.datetime.serializers.LocalDateIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import java.time.DateTimeException import java.time.format.DateTimeParseException import java.time.temporal.ChronoUnit import java.time.LocalDate as jtLocalDate -@Serializable(with = LocalDateIso8601Serializer::class) +@Serializable(with = LocalDateSerializer::class) public actual class LocalDate internal constructor(internal val value: jtLocalDate) : Comparable { public actual companion object { public actual fun parse(input: CharSequence, format: DateTimeFormat): LocalDate = diff --git a/core/jvm/src/LocalDateTime.kt b/core/jvm/src/LocalDateTime.kt index 7dc28cdb..54f2b3de 100644 --- a/core/jvm/src/LocalDateTime.kt +++ b/core/jvm/src/LocalDateTime.kt @@ -6,7 +6,7 @@ package kotlinx.datetime import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import java.time.DateTimeException import java.time.format.DateTimeParseException @@ -15,7 +15,7 @@ import java.time.LocalDateTime as jtLocalDateTime public actual typealias Month = java.time.Month public actual typealias DayOfWeek = java.time.DayOfWeek -@Serializable(with = LocalDateTimeIso8601Serializer::class) +@Serializable(with = LocalDateTimeSerializer::class) public actual class LocalDateTime internal constructor(internal val value: jtLocalDateTime) : Comparable { public actual constructor(year: Int, monthNumber: Int, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) : diff --git a/core/jvm/src/LocalTime.kt b/core/jvm/src/LocalTime.kt index 71052570..fe67736d 100644 --- a/core/jvm/src/LocalTime.kt +++ b/core/jvm/src/LocalTime.kt @@ -8,13 +8,13 @@ package kotlinx.datetime import kotlinx.datetime.format.* import kotlinx.datetime.internal.* -import kotlinx.datetime.serializers.LocalTimeIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import java.time.DateTimeException import java.time.format.DateTimeParseException import java.time.LocalTime as jtLocalTime -@Serializable(with = LocalTimeIso8601Serializer::class) +@Serializable(with = LocalTimeSerializer::class) public actual class LocalTime internal constructor(internal val value: jtLocalTime) : Comparable { diff --git a/core/jvm/src/UtcOffsetJvm.kt b/core/jvm/src/UtcOffsetJvm.kt index 129857d7..aa02495c 100644 --- a/core/jvm/src/UtcOffsetJvm.kt +++ b/core/jvm/src/UtcOffsetJvm.kt @@ -6,13 +6,14 @@ package kotlinx.datetime import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.UtcOffsetSerializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import java.time.DateTimeException import java.time.ZoneOffset import java.time.format.DateTimeFormatterBuilder import java.time.format.* +@Suppress("DEPRECATION") @Serializable(with = UtcOffsetSerializer::class) public actual class UtcOffset(internal val zoneOffset: ZoneOffset) { public actual val totalSeconds: Int get() = zoneOffset.totalSeconds diff --git a/core/native/src/Instant.kt b/core/native/src/Instant.kt index 70e0017b..96185694 100644 --- a/core/native/src/Instant.kt +++ b/core/native/src/Instant.kt @@ -10,7 +10,7 @@ package kotlinx.datetime import kotlinx.datetime.format.* import kotlinx.datetime.internal.* -import kotlinx.datetime.serializers.InstantIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlin.time.* import kotlin.time.Duration.Companion.nanoseconds @@ -38,7 +38,7 @@ private const val MAX_SECOND = 31494816403199L // +1000000-12-31T23:59:59 private fun isValidInstantSecond(second: Long) = second >= MIN_SECOND && second <= MAX_SECOND -@Serializable(with = InstantIso8601Serializer::class) +@Serializable(with = InstantSerializer::class) public actual class Instant internal constructor(public actual val epochSeconds: Long, public actual val nanosecondsOfSecond: Int) : Comparable { init { diff --git a/core/native/src/LocalDate.kt b/core/native/src/LocalDate.kt index 14ee3a17..714bbebf 100644 --- a/core/native/src/LocalDate.kt +++ b/core/native/src/LocalDate.kt @@ -12,7 +12,7 @@ import kotlinx.datetime.format.* import kotlinx.datetime.internal.* import kotlinx.datetime.internal.safeAdd import kotlinx.datetime.internal.safeMultiply -import kotlinx.datetime.serializers.LocalDateIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlin.math.* @@ -22,7 +22,7 @@ internal const val YEAR_MAX = 999_999 private fun isValidYear(year: Int): Boolean = year >= YEAR_MIN && year <= YEAR_MAX -@Serializable(with = LocalDateIso8601Serializer::class) +@Serializable(with = LocalDateSerializer::class) public actual class LocalDate actual constructor(public actual val year: Int, public actual val monthNumber: Int, public actual val dayOfMonth: Int) : Comparable { init { diff --git a/core/native/src/LocalDateTime.kt b/core/native/src/LocalDateTime.kt index 33187d4a..3b58e470 100644 --- a/core/native/src/LocalDateTime.kt +++ b/core/native/src/LocalDateTime.kt @@ -13,7 +13,7 @@ import kotlinx.datetime.internal.* import kotlinx.datetime.serializers.* import kotlinx.serialization.* -@Serializable(with = LocalDateTimeIso8601Serializer::class) +@Serializable(with = LocalDateTimeSerializer::class) public actual class LocalDateTime public actual constructor(public actual val date: LocalDate, public actual val time: LocalTime) : Comparable { public actual companion object { diff --git a/core/native/src/LocalTime.kt b/core/native/src/LocalTime.kt index 28ebd78d..42f5c017 100644 --- a/core/native/src/LocalTime.kt +++ b/core/native/src/LocalTime.kt @@ -10,10 +10,10 @@ package kotlinx.datetime import kotlinx.datetime.internal.* import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.LocalTimeIso8601Serializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable -@Serializable(LocalTimeIso8601Serializer::class) +@Serializable(LocalTimeSerializer::class) public actual class LocalTime actual constructor( public actual val hour: Int, public actual val minute: Int, diff --git a/core/native/src/UtcOffset.kt b/core/native/src/UtcOffset.kt index abe8c64d..6213360b 100644 --- a/core/native/src/UtcOffset.kt +++ b/core/native/src/UtcOffset.kt @@ -7,10 +7,11 @@ package kotlinx.datetime import kotlinx.datetime.internal.* import kotlinx.datetime.format.* -import kotlinx.datetime.serializers.UtcOffsetSerializer +import kotlinx.datetime.serializers.* import kotlinx.serialization.Serializable import kotlin.math.abs +@Suppress("DEPRECATION") @Serializable(with = UtcOffsetSerializer::class) public actual class UtcOffset private constructor(public actual val totalSeconds: Int) { diff --git a/serialization/common/test/LocalDateTimeSerializationTest.kt b/serialization/common/test/LocalDateTimeSerializationTest.kt index c01e647c..38cdbec8 100644 --- a/serialization/common/test/LocalDateTimeSerializationTest.kt +++ b/serialization/common/test/LocalDateTimeSerializationTest.kt @@ -14,6 +14,19 @@ import kotlin.test.* class LocalDateTimeSerializationTest { private fun iso8601Serialization(serializer: KSerializer) { + for ((localDateTime, json) in listOf( + Pair(LocalDateTime(2008, 7, 5, 2, 1), "\"2008-07-05T02:01:00\""), + Pair(LocalDateTime(2007, 12, 31, 23, 59, 1), "\"2007-12-31T23:59:01\""), + Pair(LocalDateTime(999, 12, 31, 23, 59, 59, 990000000), "\"0999-12-31T23:59:59.99\""), + Pair(LocalDateTime(-1, 1, 2, 23, 59, 59, 999990000), "\"-0001-01-02T23:59:59.99999\""), + Pair(LocalDateTime(-2008, 1, 2, 23, 59, 59, 999999990), "\"-2008-01-02T23:59:59.99999999\""), + )) { + assertEquals(json, Json.encodeToString(serializer, localDateTime)) + assertEquals(localDateTime, Json.decodeFromString(serializer, json)) + } + } + + private fun defaultSerialization(serializer: KSerializer) { for ((localDateTime, json) in listOf( Pair(LocalDateTime(2008, 7, 5, 2, 1), "\"2008-07-05T02:01\""), Pair(LocalDateTime(2007, 12, 31, 23, 59, 1), "\"2007-12-31T23:59:01\""), @@ -79,7 +92,6 @@ class LocalDateTimeSerializationTest { @Test fun testDefaultSerializers() { - // should be the same as the ISO 8601 - iso8601Serialization(Json.serializersModule.serializer()) + defaultSerialization(Json.serializersModule.serializer()) } } diff --git a/serialization/common/test/LocalTimeSerializationTest.kt b/serialization/common/test/LocalTimeSerializationTest.kt index 5df81f54..6147e31f 100644 --- a/serialization/common/test/LocalTimeSerializationTest.kt +++ b/serialization/common/test/LocalTimeSerializationTest.kt @@ -14,6 +14,19 @@ import kotlin.test.* class LocalTimeSerializationTest { private fun iso8601Serialization(serializer: KSerializer) { + for ((localTime, json) in listOf( + Pair(LocalTime(2, 1), "\"02:01:00\""), + Pair(LocalTime(23, 59, 1), "\"23:59:01\""), + Pair(LocalTime(23, 59, 59, 990000000), "\"23:59:59.99\""), + Pair(LocalTime(23, 59, 59, 999990000), "\"23:59:59.99999\""), + Pair(LocalTime(23, 59, 59, 999999990), "\"23:59:59.99999999\""), + )) { + assertEquals(json, Json.encodeToString(serializer, localTime)) + assertEquals(localTime, Json.decodeFromString(serializer, json)) + } + } + + private fun defaultSerialization(serializer: KSerializer) { for ((localTime, json) in listOf( Pair(LocalTime(2, 1), "\"02:01\""), Pair(LocalTime(23, 59, 1), "\"23:59:01\""), @@ -69,7 +82,6 @@ class LocalTimeSerializationTest { @Test fun testDefaultSerializers() { - // should be the same as the ISO 8601 - iso8601Serialization(Json.serializersModule.serializer()) + defaultSerialization(Json.serializersModule.serializer()) } } diff --git a/serialization/common/test/UtcOffsetSerializationTest.kt b/serialization/common/test/UtcOffsetSerializationTest.kt index 2504d6fb..8b37d2e3 100644 --- a/serialization/common/test/UtcOffsetSerializationTest.kt +++ b/serialization/common/test/UtcOffsetSerializationTest.kt @@ -14,25 +14,46 @@ import kotlin.test.* class UtcOffsetSerializationTest { - private fun testSerializationAsPrimitive(serializer: KSerializer) { - val offset2h = UtcOffset(hours = 2) - assertEquals("\"+02:00\"", Json.encodeToString(serializer, offset2h)) - assertEquals(offset2h, Json.decodeFromString(serializer, "\"+02:00\"")) - assertEquals(offset2h, Json.decodeFromString(serializer, "\"+02:00:00\"")) - - assertFailsWith { - Json.decodeFromString(serializer, "\"UTC+02:00\"") // not an offset + private fun iso8601Serialization(serializer: KSerializer) { + // the default form is obtainable and parsable + for ((offset, json) in listOf( + Pair(UtcOffset(hours = 0), "\"Z\""), + Pair(UtcOffset(hours = 1), "\"+01:00\""), + Pair(UtcOffset(hours = 1, minutes = 30), "\"+01:30\""), + Pair(UtcOffset(hours = 1, minutes = 30, seconds = 59), "\"+01:30:59\""), + )) { + assertEquals(json, Json.encodeToString(serializer, offset)) + assertEquals(offset, Json.decodeFromString(serializer, json)) + } + // alternative forms are also parsable + for ((offset, json) in listOf( + Pair(UtcOffset(hours = 0), "\"+00:00\""), + Pair(UtcOffset(hours = 0), "\"z\""), + )) { + assertEquals(offset, Json.decodeFromString(serializer, json)) + } + // some strings aren't parsable + for (json in listOf( + "\"+3\"", + "\"+03\"", + "\"+03:0\"", + "\"UTC+02:00\"", + )) { + assertFailsWith { + Json.decodeFromString(serializer, json) + } } } @Test - fun defaultSerializer() { - testSerializationAsPrimitive(Json.serializersModule.serializer()) + fun testIso8601Serialization() { + iso8601Serialization(UtcOffsetIso8601Serializer) } @Test - fun stringPrimitiveSerializer() { - testSerializationAsPrimitive(UtcOffsetSerializer) - testSerializationAsPrimitive(UtcOffset.serializer()) + fun testDefaultSerializers() { + // should be the same as the ISO 8601 + iso8601Serialization(Json.serializersModule.serializer()) + iso8601Serialization(UtcOffset.serializer()) } }