Skip to content

Commit 06ed460

Browse files
committed
Migrate from kotlinx.serialization internal API to public one where applicable
1 parent dbb153f commit 06ed460

7 files changed

+49
-58
lines changed

core/common/src/serializers/DateTimeUnitSerializers.kt

Lines changed: 14 additions & 13 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

@@ -32,7 +32,6 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
3232
}
3333

3434
@OptIn(ExperimentalSerializationApi::class)
35-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
3635
override fun deserialize(decoder: Decoder): DateTimeUnit.TimeBased {
3736
var seen = false
3837
var nanoseconds = 0L
@@ -48,12 +47,12 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
4847
seen = true
4948
}
5049
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
51-
else -> throw UnknownFieldException(elementIndex)
50+
else -> throwUnknownIndexException(elementIndex)
5251
}
5352
}
5453
}
5554
}
56-
if (!seen) throw MissingFieldException("nanoseconds")
55+
if (!seen) throw MissingFieldException(missingField = "nanoseconds", serialName = descriptor.serialName)
5756
return DateTimeUnit.TimeBased(nanoseconds)
5857
}
5958
}
@@ -76,7 +75,6 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
7675
}
7776

7877
@OptIn(ExperimentalSerializationApi::class)
79-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
8078
override fun deserialize(decoder: Decoder): DateTimeUnit.DayBased {
8179
var seen = false
8280
var days = 0
@@ -92,12 +90,12 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
9290
seen = true
9391
}
9492
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
95-
else -> throw UnknownFieldException(elementIndex)
93+
else -> throwUnknownIndexException(elementIndex)
9694
}
9795
}
9896
}
9997
}
100-
if (!seen) throw MissingFieldException("days")
98+
if (!seen) throw MissingFieldException(missingField = "days", serialName = TimeBasedDateTimeUnitSerializer.descriptor.serialName)
10199
return DateTimeUnit.DayBased(days)
102100
}
103101
}
@@ -120,7 +118,6 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
120118
}
121119

122120
@OptIn(ExperimentalSerializationApi::class)
123-
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
124121
override fun deserialize(decoder: Decoder): DateTimeUnit.MonthBased {
125122
var seen = false
126123
var months = 0
@@ -136,12 +133,12 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
136133
seen = true
137134
}
138135
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
139-
else -> throw UnknownFieldException(elementIndex)
136+
else -> throwUnknownIndexException(elementIndex)
140137
}
141138
}
142139
}
143140
}
144-
if (!seen) throw MissingFieldException("months")
141+
if (!seen) throw MissingFieldException(missingField = "months", serialName = descriptor.serialName)
145142
return DateTimeUnit.MonthBased(months)
146143
}
147144
}
@@ -151,7 +148,7 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
151148
*
152149
* JSON example: `{"type":"DayBased","days":15}`
153150
*/
154-
@Suppress("EXPERIMENTAL_API_USAGE_ERROR", "INVISIBLE_MEMBER")
151+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") // https://github.com/Kotlin/kotlinx.serialization/issues/2460
155152
@OptIn(InternalSerializationApi::class)
156153
public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit.DateBased>() {
157154

@@ -185,9 +182,9 @@ public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<Dat
185182
*
186183
* JSON example: `{"type":"MonthBased","days":15}`
187184
*/
188-
@Suppress("EXPERIMENTAL_API_USAGE_ERROR", "INVISIBLE_MEMBER")
185+
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") // https://github.com/Kotlin/kotlinx.serialization/issues/2460
189186
@OptIn(InternalSerializationApi::class)
190-
public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit>() {
187+
public object DateTimeUnitSerializer : AbstractPolymorphicSerializer<DateTimeUnit>() {
191188

192189
private val impl = SealedClassSerializer("kotlinx.datetime.DateTimeUnit",
193190
DateTimeUnit::class,
@@ -210,3 +207,7 @@ public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit
210207
get() = impl.descriptor
211208

212209
}
210+
211+
internal fun throwUnknownIndexException(index: Int): Nothing {
212+
throw SerializationException("An unknown field for index $index")
213+
}
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)