-
Notifications
You must be signed in to change notification settings - Fork 124
Deprecate TimeZone serialization #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,6 @@ import kotlin.time.Instant | |
| * | ||
| * @sample kotlinx.datetime.test.samples.TimeZoneSamples.usage | ||
| */ | ||
| @Serializable(with = TimeZoneSerializer::class) | ||
| public expect open class TimeZone { | ||
| /** | ||
| * Returns the identifier string of the time zone. | ||
|
|
@@ -163,6 +162,15 @@ public expect open class TimeZone { | |
| * @sample kotlinx.datetime.test.samples.TimeZoneSamples.availableZoneIds | ||
| */ | ||
| public val availableZoneIds: Set<String> | ||
|
|
||
| /** @suppress */ | ||
| @Deprecated( | ||
| "Serializing TimeZone is discouraged, " + | ||
| "as deserialization can fail depending on the configuration. " + | ||
| "Please serialize the string id instead.", | ||
| level = DeprecationLevel.WARNING, | ||
| ) | ||
| public fun serializer(): kotlinx.serialization.KSerializer<TimeZone> | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -235,7 +243,6 @@ public expect open class TimeZone { | |
| * | ||
| * @sample kotlinx.datetime.test.samples.TimeZoneSamples.FixedOffsetTimeZoneSamples.casting | ||
| */ | ||
| @Serializable(with = FixedOffsetTimeZoneSerializer::class) | ||
| public expect class FixedOffsetTimeZone : TimeZone { | ||
| /** | ||
| * Constructs a time zone with the fixed [offset] from UTC. | ||
|
|
@@ -253,6 +260,18 @@ public expect class FixedOffsetTimeZone : TimeZone { | |
|
|
||
| @Deprecated("Use offset.totalSeconds", ReplaceWith("offset.totalSeconds")) | ||
| public val totalSeconds: Int | ||
|
|
||
| /** @suppress */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suppress what? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The generation of the KDoc for this object. |
||
| public companion object { | ||
| /** @suppress */ | ||
| @Deprecated( | ||
| "Serializing FixedOffsetTimeZone is discouraged, " + | ||
| "as deserialization can fail or return a non-fixed-offset zone depending on the configuration. " + | ||
| "Please serialize the string id instead.", | ||
| level = DeprecationLevel.WARNING, | ||
| ) | ||
| public fun serializer(): kotlinx.serialization.KSerializer<FixedOffsetTimeZone> | ||
| } | ||
| } | ||
|
|
||
| @Deprecated("Use FixedOffsetTimeZone or UtcOffset instead", ReplaceWith("FixedOffsetTimeZone")) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,11 @@ | ||
| /* | ||
| * Copyright 2019-2021 JetBrains s.r.o. | ||
| * Copyright 2025 JetBrains s.r.o. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, we can leave only the start date, not the end date There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove the copyright notices completely: Kotlin/kotlinx.coroutines#4016 |
||
| * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. | ||
| */ | ||
|
|
||
| package kotlinx.datetime.serializers | ||
|
|
||
| import kotlinx.datetime.* | ||
| import kotlinx.datetime.format.DateTimeFormat | ||
| import kotlinx.serialization.* | ||
| import kotlinx.serialization.descriptors.* | ||
| import kotlinx.serialization.encoding.* | ||
|
|
@@ -16,6 +15,10 @@ import kotlinx.serialization.encoding.* | |
| * | ||
| * JSON example: `"Europe/Berlin"` | ||
| */ | ||
| @Deprecated( | ||
| "Serializing TimeZone is discouraged. Please serialize the string id instead.", | ||
| level = DeprecationLevel.WARNING, | ||
| ) | ||
| public object TimeZoneSerializer: KSerializer<TimeZone> { | ||
|
|
||
| override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.TimeZone", PrimitiveKind.STRING) | ||
|
|
@@ -33,6 +36,10 @@ public object TimeZoneSerializer: KSerializer<TimeZone> { | |
| * | ||
| * JSON example: `"+02:00"` | ||
| */ | ||
| @Deprecated( | ||
| "Serializing FixedOffsetTimeZoneSerializer is discouraged. Please serialize the string id instead.", | ||
| level = DeprecationLevel.WARNING, | ||
| ) | ||
| public object FixedOffsetTimeZoneSerializer: KSerializer<FixedOffsetTimeZone> { | ||
|
|
||
| override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.FixedOffsetTimeZone", PrimitiveKind.STRING) | ||
|
|
@@ -51,60 +58,3 @@ public object FixedOffsetTimeZoneSerializer: KSerializer<FixedOffsetTimeZone> { | |
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * A serializer for [UtcOffset] that uses the extended ISO 8601 representation. | ||
| * | ||
| * JSON example: `"+02:00"` | ||
| * | ||
| * @see UtcOffset.Formats.ISO | ||
| */ | ||
| public object UtcOffsetIso8601Serializer : KSerializer<UtcOffset> | ||
| by UtcOffset.Formats.ISO.asKSerializer("kotlinx.datetime.UtcOffset/ISO") | ||
|
|
||
| /** | ||
| * 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<UtcOffset> { | ||
|
|
||
| override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.UtcOffset", PrimitiveKind.STRING) | ||
|
|
||
| override fun deserialize(decoder: Decoder): UtcOffset { | ||
| return UtcOffset.parse(decoder.decodeString()) | ||
| } | ||
|
|
||
| override fun serialize(encoder: Encoder, value: UtcOffset) { | ||
| encoder.encodeString(value.toString()) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * An abstract serializer for [UtcOffset] values that uses | ||
| * a custom [DateTimeFormat] to serialize and deserialize the value. | ||
| * | ||
| * [name] is the name of the serializer. | ||
| * The [SerialDescriptor.serialName] of the resulting serializer is `kotlinx.datetime.UtcOffset/serializer/`[name]. | ||
| * [SerialDescriptor.serialName] must be unique across all serializers in the same serialization context. | ||
| * When defining a serializer in a library, it is recommended to use the fully qualified class name in [name] | ||
| * to avoid conflicts with serializers defined by other libraries and client code. | ||
| * | ||
| * This serializer is abstract and must be subclassed to provide a concrete serializer. | ||
| * Example: | ||
| * ``` | ||
| * // serializes the UTC offset UtcOffset(hours = 2) as the string "+0200" | ||
| * object FourDigitOffsetSerializer : FormattedUtcOffsetSerializer( | ||
| * "my.package.FOUR_DIGITS", UtcOffset.Formats.FOUR_DIGITS | ||
| * ) | ||
| * ``` | ||
| * | ||
| * Note that [UtcOffset] is [kotlinx.serialization.Serializable] by default, | ||
| * so it is not necessary to create custom serializers when the format is not important. | ||
| * Additionally, [UtcOffsetSerializer] is provided for the ISO 8601 format. | ||
| */ | ||
| public abstract class FormattedUtcOffsetSerializer( | ||
| name: String, format: DateTimeFormat<UtcOffset> | ||
| ) : KSerializer<UtcOffset> by format.asKSerializer("kotlinx.datetime.UtcOffset/serializer/$name") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright 2019-2021 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. | ||
| */ | ||
|
|
||
| package kotlinx.datetime.serializers | ||
|
|
||
| import kotlinx.datetime.* | ||
| import kotlinx.datetime.format.DateTimeFormat | ||
| import kotlinx.serialization.* | ||
| import kotlinx.serialization.descriptors.* | ||
| import kotlinx.serialization.encoding.* | ||
|
|
||
| /** | ||
| * A serializer for [UtcOffset] that uses the extended ISO 8601 representation. | ||
| * | ||
| * JSON example: `"+02:00"` | ||
| * | ||
| * @see UtcOffset.Formats.ISO | ||
| */ | ||
| public object UtcOffsetIso8601Serializer : KSerializer<UtcOffset> | ||
| by UtcOffset.Formats.ISO.asKSerializer("kotlinx.datetime.UtcOffset/ISO") | ||
|
|
||
| /** | ||
| * 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<UtcOffset> { | ||
|
|
||
| override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlinx.datetime.UtcOffset", PrimitiveKind.STRING) | ||
|
|
||
| override fun deserialize(decoder: Decoder): UtcOffset { | ||
| return UtcOffset.parse(decoder.decodeString()) | ||
| } | ||
|
|
||
| override fun serialize(encoder: Encoder, value: UtcOffset) { | ||
| encoder.encodeString(value.toString()) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * An abstract serializer for [UtcOffset] values that uses | ||
| * a custom [DateTimeFormat] to serialize and deserialize the value. | ||
| * | ||
| * [name] is the name of the serializer. | ||
| * The [SerialDescriptor.serialName] of the resulting serializer is `kotlinx.datetime.UtcOffset/serializer/`[name]. | ||
| * [SerialDescriptor.serialName] must be unique across all serializers in the same serialization context. | ||
| * When defining a serializer in a library, it is recommended to use the fully qualified class name in [name] | ||
| * to avoid conflicts with serializers defined by other libraries and client code. | ||
| * | ||
| * This serializer is abstract and must be subclassed to provide a concrete serializer. | ||
| * Example: | ||
| * ``` | ||
| * // serializes the UTC offset UtcOffset(hours = 2) as the string "+0200" | ||
| * object FourDigitOffsetSerializer : FormattedUtcOffsetSerializer( | ||
| * "my.package.FOUR_DIGITS", UtcOffset.Formats.FOUR_DIGITS | ||
| * ) | ||
| * ``` | ||
| * | ||
| * Note that [UtcOffset] is [kotlinx.serialization.Serializable] by default, | ||
| * so it is not necessary to create custom serializers when the format is not important. | ||
| * Additionally, [UtcOffsetSerializer] is provided for the ISO 8601 format. | ||
| */ | ||
| public abstract class FormattedUtcOffsetSerializer( | ||
| name: String, format: DateTimeFormat<UtcOffset> | ||
| ) : KSerializer<UtcOffset> by format.asKSerializer("kotlinx.datetime.UtcOffset/serializer/$name") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the moment you remove
@Serializable(with=...)annotation from type, it is shown as red with SERIALIZER_NOT_FOUND where it was used in any@Serializableuser class. Sadly, we do not have any kind of deprecation migration for that; so I suggest raising all@Deprecatedhere to ERROR just to speedup this processUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is to keep it
WARNINGfor now so that people can work around theSERIALIZER_NOT_FOUNDissue by adding their own annotation to theTimeZonefield. Then, after a migratory period, when we expect such annotations to be removed as well, the deprecation can be promoted toERROR.Does this approach make sense?