|
5 | 5 |
|
6 | 6 | package kotlinx.datetime.serializers |
7 | 7 |
|
8 | | -import kotlinx.datetime.Instant |
9 | | -import kotlinx.datetime.format |
| 8 | +import kotlinx.datetime.* |
10 | 9 | import kotlinx.datetime.format.DateTimeComponents |
11 | 10 | import kotlinx.datetime.format.DateTimeFormat |
12 | 11 | import kotlinx.serialization.* |
@@ -86,30 +85,43 @@ public object InstantComponentSerializer : KSerializer<Instant> { |
86 | 85 | * [format] should be a format that includes enough components to unambiguously define a date, a time, and a UTC offset. |
87 | 86 | * See [Instant.parse] for details of how deserialization is performed. |
88 | 87 | * |
| 88 | + * [name] is the name of the serializer. |
| 89 | + * The [SerialDescriptor.serialName] of the resulting serializer is `kotlinx.datetime.Instant serializer `[name]. |
| 90 | + * [SerialDescriptor.serialName] must be unique across all serializers in the same serialization context. |
| 91 | + * When defining a serializer in a library, it is recommended to use the fully qualified class name in [name] |
| 92 | + * to avoid conflicts with serializers defined by other libraries and client code. |
| 93 | + * |
89 | 94 | * When serializing, the [Instant] value is formatted as a string using the specified [format] |
90 | 95 | * in the [ZERO][UtcOffset.ZERO] UTC offset. |
91 | 96 | * |
92 | 97 | * This serializer is abstract and must be subclassed to provide a concrete serializer. |
93 | 98 | * Example: |
94 | 99 | * ``` |
95 | | - * object Rfc1123InstantSerializer : FormattedInstantSerializer(DateTimeComponents.Formats.RFC_1123) |
| 100 | + * // serializes LocalDateTime(2008, 6, 30, 11, 5, 30).toInstant(TimeZone.UTC) |
| 101 | + * // as the string "Mon, 30 Jun 2008 11:05:30 GMT" |
| 102 | + * object Rfc1123InstantSerializer : FormattedInstantSerializer( |
| 103 | + * "my.package.RFC1123", DateTimeComponents.Formats.RFC_1123 |
| 104 | + * ) |
96 | 105 | * ``` |
97 | 106 | * |
98 | 107 | * Note that [Instant] is [kotlinx.serialization.Serializable] by default, |
99 | 108 | * so it is not necessary to create custom serializers when the format is not important. |
100 | 109 | * Additionally, [InstantIso8601Serializer] is provided for the ISO 8601 format. |
101 | 110 | */ |
102 | 111 | public abstract class FormattedInstantSerializer( |
| 112 | + name: String, |
103 | 113 | private val format: DateTimeFormat<DateTimeComponents>, |
104 | 114 | ) : KSerializer<Instant> { |
105 | | - |
106 | 115 | override val descriptor: SerialDescriptor = |
107 | | - PrimitiveSerialDescriptor("kotlinx.datetime.Instant", PrimitiveKind.STRING) |
| 116 | + PrimitiveSerialDescriptor("kotlinx.datetime.Instant serializer $name", PrimitiveKind.STRING) |
108 | 117 |
|
109 | 118 | override fun deserialize(decoder: Decoder): Instant = |
110 | 119 | Instant.parse(decoder.decodeString(), format) |
111 | 120 |
|
112 | 121 | override fun serialize(encoder: Encoder, value: Instant) { |
113 | 122 | encoder.encodeString(value.format(format)) |
114 | 123 | } |
| 124 | + |
| 125 | + @OptIn(ExperimentalSerializationApi::class) |
| 126 | + override fun toString(): String = descriptor.serialName |
115 | 127 | } |
0 commit comments