Skip to content

Commit bc78bca

Browse files
committed
Migrate from kotlinx.serialization internal API to public one where applicable
1 parent d58a4aa commit bc78bca

7 files changed

+50
-59
lines changed

core/common/src/serializers/DateTimeUnitSerializers.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 JetBrains s.r.o.
2+
* Copyright 2019-2023 JetBrains s.r.o.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

@@ -35,7 +35,6 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
3535
}
3636

3737
@OptIn(ExperimentalSerializationApi::class)
38-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
3938
override fun deserialize(decoder: Decoder): DateTimeUnit.TimeBased {
4039
var seen = false
4140
var nanoseconds = 0L
@@ -51,12 +50,12 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
5150
seen = true
5251
}
5352
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
54-
else -> throw UnknownFieldException(elementIndex)
53+
else -> throwUnknownIndexException(elementIndex)
5554
}
5655
}
5756
}
5857
}
59-
if (!seen) throw MissingFieldException("nanoseconds")
58+
if (!seen) throw MissingFieldException(missingField = "nanoseconds", serialName = descriptor.serialName)
6059
return DateTimeUnit.TimeBased(nanoseconds)
6160
}
6261
}
@@ -82,7 +81,6 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
8281
}
8382

8483
@OptIn(ExperimentalSerializationApi::class)
85-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
8684
override fun deserialize(decoder: Decoder): DateTimeUnit.DayBased {
8785
var seen = false
8886
var days = 0
@@ -98,12 +96,12 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
9896
seen = true
9997
}
10098
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
101-
else -> throw UnknownFieldException(elementIndex)
99+
else -> throwUnknownIndexException(elementIndex)
102100
}
103101
}
104102
}
105103
}
106-
if (!seen) throw MissingFieldException("days")
104+
if (!seen) throw MissingFieldException(missingField = "days", serialName = TimeBasedDateTimeUnitSerializer.descriptor.serialName)
107105
return DateTimeUnit.DayBased(days)
108106
}
109107
}
@@ -129,7 +127,6 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
129127
}
130128

131129
@OptIn(ExperimentalSerializationApi::class)
132-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
133130
override fun deserialize(decoder: Decoder): DateTimeUnit.MonthBased {
134131
var seen = false
135132
var months = 0
@@ -145,12 +142,12 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
145142
seen = true
146143
}
147144
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
148-
else -> throw UnknownFieldException(elementIndex)
145+
else -> throwUnknownIndexException(elementIndex)
149146
}
150147
}
151148
}
152149
}
153-
if (!seen) throw MissingFieldException("months")
150+
if (!seen) throw MissingFieldException(missingField = "months", serialName = descriptor.serialName)
154151
return DateTimeUnit.MonthBased(months)
155152
}
156153
}
@@ -160,7 +157,7 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
160157
*
161158
* JSON example: `{"type":"DayBased","days":15}`
162159
*/
163-
@Suppress("EXPERIMENTAL_API_USAGE_ERROR", "INVISIBLE_MEMBER")
160+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") // https://github.com/Kotlin/kotlinx.serialization/issues/2460
164161
@OptIn(InternalSerializationApi::class)
165162
public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit.DateBased>() {
166163

@@ -197,9 +194,9 @@ public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<Dat
197194
*
198195
* JSON example: `{"type":"MonthBased","days":15}`
199196
*/
200-
@Suppress("EXPERIMENTAL_API_USAGE_ERROR", "INVISIBLE_MEMBER")
197+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") // https://github.com/Kotlin/kotlinx.serialization/issues/2460
201198
@OptIn(InternalSerializationApi::class)
202-
public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit>() {
199+
public object DateTimeUnitSerializer : AbstractPolymorphicSerializer<DateTimeUnit>() {
203200

204201
// https://youtrack.jetbrains.com/issue/KT-63939
205202
private val impl by lazy(LazyThreadSafetyMode.PUBLICATION) {
@@ -224,4 +221,8 @@ public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit
224221
override val descriptor: SerialDescriptor
225222
get() = impl.descriptor
226223

227-
}
224+
}
225+
226+
internal fun throwUnknownIndexException(index: Int): Nothing {
227+
throw SerializationException("An unknown field for index $index")
228+
}
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2019-2021 JetBrains s.r.o.
2+
* Copyright 2019-2023 JetBrains s.r.o.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

66
package kotlinx.datetime.serializers
77

8-
import kotlinx.datetime.DayOfWeek
8+
import kotlinx.datetime.*
99
import kotlinx.serialization.*
1010
import kotlinx.serialization.descriptors.*
1111
import kotlinx.serialization.encoding.*
@@ -16,14 +16,7 @@ import kotlinx.serialization.internal.*
1616
*
1717
* JSON example: `"MONDAY"`
1818
*/
19-
@Suppress("INVISIBLE_MEMBER")
20-
public object DayOfWeekSerializer: KSerializer<DayOfWeek> {
21-
private val impl = EnumSerializer("Month", DayOfWeek.values())
22-
23-
override val descriptor: SerialDescriptor
24-
get() = impl.descriptor
25-
26-
override fun deserialize(decoder: Decoder): DayOfWeek = impl.deserialize(decoder)
27-
28-
override fun serialize(encoder: Encoder, value: DayOfWeek): Unit = impl.serialize(encoder, value)
29-
}
19+
public object DayOfWeekSerializer : KSerializer<DayOfWeek> by createEnumSerializer<DayOfWeek>(
20+
"kotlinx.datetime.serializers.DayOfWeek",
21+
DayOfWeek.values()
22+
)

core/common/src/serializers/InstantSerializers.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 JetBrains s.r.o.
2+
* Copyright 2019-2023 JetBrains s.r.o.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

@@ -18,7 +18,7 @@ import kotlinx.serialization.encoding.*
1818
* @see Instant.toString
1919
* @see Instant.parse
2020
*/
21-
public object InstantIso8601Serializer: KSerializer<Instant> {
21+
public object InstantIso8601Serializer : KSerializer<Instant> {
2222

2323
override val descriptor: SerialDescriptor =
2424
PrimitiveSerialDescriptor("Instant", PrimitiveKind.STRING)
@@ -37,7 +37,7 @@ public object InstantIso8601Serializer: KSerializer<Instant> {
3737
*
3838
* JSON example: `{"epochSeconds":1607505416,"nanosecondsOfSecond":124000}`
3939
*/
40-
public object InstantComponentSerializer: KSerializer<Instant> {
40+
public object InstantComponentSerializer : KSerializer<Instant> {
4141

4242
override val descriptor: SerialDescriptor =
4343
buildClassSerialDescriptor("Instant") {
@@ -46,20 +46,22 @@ public object InstantComponentSerializer: KSerializer<Instant> {
4646
}
4747

4848
@OptIn(ExperimentalSerializationApi::class)
49-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
5049
override fun deserialize(decoder: Decoder): Instant =
5150
decoder.decodeStructure(descriptor) {
5251
var epochSeconds: Long? = null
5352
var nanosecondsOfSecond = 0
54-
loop@while (true) {
53+
loop@ while (true) {
5554
when (val index = decodeElementIndex(descriptor)) {
5655
0 -> epochSeconds = decodeLongElement(descriptor, 0)
5756
1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1)
5857
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
5958
else -> throw SerializationException("Unexpected index: $index")
6059
}
6160
}
62-
if (epochSeconds == null) throw MissingFieldException("epochSeconds")
61+
if (epochSeconds == null) throw MissingFieldException(
62+
missingField = "epochSeconds",
63+
serialName = descriptor.serialName
64+
)
6365
Instant.fromEpochSeconds(epochSeconds, nanosecondsOfSecond)
6466
}
6567

core/common/src/serializers/LocalDateSerializers.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 JetBrains s.r.o.
2+
* Copyright 2019-2023 JetBrains s.r.o.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

@@ -47,7 +47,6 @@ public object LocalDateComponentSerializer: KSerializer<LocalDate> {
4747
}
4848

4949
@OptIn(ExperimentalSerializationApi::class)
50-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
5150
override fun deserialize(decoder: Decoder): LocalDate =
5251
decoder.decodeStructure(descriptor) {
5352
var year: Int? = null
@@ -59,12 +58,12 @@ public object LocalDateComponentSerializer: KSerializer<LocalDate> {
5958
1 -> month = decodeShortElement(descriptor, 1)
6059
2 -> day = decodeShortElement(descriptor, 2)
6160
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
62-
else -> throw SerializationException("Unexpected index: $index")
61+
else -> throwUnknownIndexException(index)
6362
}
6463
}
65-
if (year == null) throw MissingFieldException("year")
66-
if (month == null) throw MissingFieldException("month")
67-
if (day == null) throw MissingFieldException("day")
64+
if (year == null) throw MissingFieldException(missingField = "year", serialName = descriptor.serialName)
65+
if (month == null) throw MissingFieldException(missingField = "month", serialName = descriptor.serialName)
66+
if (day == null) throw MissingFieldException(missingField = "day", serialName = descriptor.serialName)
6867
LocalDate(year, month.toInt(), day.toInt())
6968
}
7069

core/common/src/serializers/LocalDateTimeSerializers.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 JetBrains s.r.o.
2+
* Copyright 2019-2023 JetBrains s.r.o.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

@@ -51,7 +51,6 @@ public object LocalDateTimeComponentSerializer: KSerializer<LocalDateTime> {
5151
}
5252

5353
@OptIn(ExperimentalSerializationApi::class)
54-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
5554
override fun deserialize(decoder: Decoder): LocalDateTime =
5655
decoder.decodeStructure(descriptor) {
5756
var year: Int? = null
@@ -74,11 +73,11 @@ public object LocalDateTimeComponentSerializer: KSerializer<LocalDateTime> {
7473
else -> throw SerializationException("Unexpected index: $index")
7574
}
7675
}
77-
if (year == null) throw MissingFieldException("year")
78-
if (month == null) throw MissingFieldException("month")
79-
if (day == null) throw MissingFieldException("day")
80-
if (hour == null) throw MissingFieldException("hour")
81-
if (minute == null) throw MissingFieldException("minute")
76+
if (year == null) throw MissingFieldException(missingField = "year", serialName = descriptor.serialName)
77+
if (month == null) throw MissingFieldException(missingField = "month", serialName = descriptor.serialName)
78+
if (day == null) throw MissingFieldException(missingField = "day", serialName = descriptor.serialName)
79+
if (hour == null) throw MissingFieldException(missingField = "hour", serialName = descriptor.serialName)
80+
if (minute == null) throw MissingFieldException(missingField = "minute", serialName = descriptor.serialName)
8281
LocalDateTime(year, month.toInt(), day.toInt(), hour.toInt(), minute.toInt(), second.toInt(), nanosecond)
8382
}
8483

core/common/src/serializers/LocalTimeSerializers.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public object LocalTimeComponentSerializer : KSerializer<LocalTime> {
4747
}
4848

4949
@OptIn(ExperimentalSerializationApi::class)
50-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
5150
override fun deserialize(decoder: Decoder): LocalTime =
5251
decoder.decodeStructure(descriptor) {
5352
var hour: Short? = null
@@ -64,8 +63,8 @@ public object LocalTimeComponentSerializer : KSerializer<LocalTime> {
6463
else -> throw SerializationException("Unexpected index: $index")
6564
}
6665
}
67-
if (hour == null) throw MissingFieldException("hour")
68-
if (minute == null) throw MissingFieldException("minute")
66+
if (hour == null) throw MissingFieldException(missingField = "hour", serialName = descriptor.serialName)
67+
if (minute == null) throw MissingFieldException(missingField = "minute", serialName = descriptor.serialName)
6968
LocalTime(hour.toInt(), minute.toInt(), second.toInt(), nanosecond)
7069
}
7170

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 JetBrains s.r.o.
2+
* Copyright 2019-2023 JetBrains s.r.o.
33
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
44
*/
55

@@ -16,14 +16,12 @@ import kotlinx.serialization.internal.*
1616
*
1717
* JSON example: `"JANUARY"`
1818
*/
19-
@Suppress("INVISIBLE_MEMBER")
20-
public object MonthSerializer: KSerializer<Month> {
21-
private val impl = EnumSerializer("Month", Month.values())
19+
public object MonthSerializer : KSerializer<Month> by createEnumSerializer<Month>(
20+
"kotlinx.datetime.serializers.Month",
21+
Month.values())
2222

23-
override val descriptor: SerialDescriptor
24-
get() = impl.descriptor
25-
26-
override fun deserialize(decoder: Decoder): Month = impl.deserialize(decoder)
27-
28-
override fun serialize(encoder: Encoder, value: Month): Unit = impl.serialize(encoder, value)
23+
// Until https://github.com/Kotlin/kotlinx.serialization/issues/2459 is resolved
24+
internal fun <E : Enum<E>> createEnumSerializer(serialName: String, values: Array<E>): KSerializer<E> {
25+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
26+
return kotlinx.serialization.internal.EnumSerializer(serialName, values)
2927
}

0 commit comments

Comments
 (0)