Skip to content

Commit 2c35bb4

Browse files
committed
Fixes
1 parent 756af5c commit 2c35bb4

13 files changed

+34
-26
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ buildscript {
33
mavenCentral()
44
}
55
dependencies {
6-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.0")
6+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30")
77
}
88
}
99

1010
plugins {
1111
id("kotlinx.team.infra") version "0.3.0-dev-64"
12-
kotlin("plugin.serialization") version "1.4.10"
12+
kotlin("plugin.serialization") version "1.4.30"
1313
}
1414

1515
infra {

core/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ base {
1919

2020
//val JDK_6: String by project
2121
val JDK_8: String by project
22+
val serializationVersion: String by project
2223

2324
kotlin {
2425
infra {
@@ -149,7 +150,7 @@ kotlin {
149150
commonMain {
150151
dependencies {
151152
api("org.jetbrains.kotlin:kotlin-stdlib-common")
152-
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.1")
153+
compileOnly("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
153154
}
154155
}
155156

core/common/src/serializers/DateTimePeriodSerializers.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object DateTimePeriodComponentSerializer: KSerializer<DateTimePeriod> {
4444
5 -> seconds = decodeIntElement(descriptor, 5)
4545
6 -> nanoseconds = decodeLongElement(descriptor, 6)
4646
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
47-
else -> error("Unexpected index: $index")
47+
else -> throw SerializationException("Unexpected index: $index")
4848
}
4949
}
5050
DateTimePeriod(years, months, days, hours, minutes, seconds, nanoseconds)
@@ -84,7 +84,7 @@ object DatePeriodComponentSerializer: KSerializer<DatePeriod> {
8484

8585
private fun unexpectedNonzero(fieldName: String, value: Long) {
8686
if (value != 0L) {
87-
throw SerializationException("expected field '$fieldName' to be zero, but was $value")
87+
throw SerializationException("DatePeriod should have non-date components be zero, but got $value in '$fieldName'")
8888
}
8989
}
9090

@@ -97,7 +97,7 @@ object DatePeriodComponentSerializer: KSerializer<DatePeriod> {
9797
element<Int>("days", isOptional = true)
9898
element<Int>("hours", isOptional = true)
9999
element<Int>("minutes", isOptional = true)
100-
element<Long>("seconds", isOptional = true)
100+
element<Int>("seconds", isOptional = true)
101101
element<Long>("nanoseconds", isOptional = true)
102102
}
103103

@@ -113,10 +113,10 @@ object DatePeriodComponentSerializer: KSerializer<DatePeriod> {
113113
2 -> days = decodeIntElement(descriptor, 2)
114114
3 -> unexpectedNonzero("hours", decodeIntElement(descriptor, 3))
115115
4 -> unexpectedNonzero("minutes", decodeIntElement(descriptor, 4))
116-
5 -> unexpectedNonzero("seconds", decodeLongElement(descriptor, 5))
116+
5 -> unexpectedNonzero("seconds", decodeIntElement(descriptor, 5))
117117
6 -> unexpectedNonzero("nanoseconds", decodeLongElement(descriptor, 6))
118118
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
119-
else -> error("Unexpected index: $index")
119+
else -> throw SerializationException("Unexpected index: $index")
120120
}
121121
}
122122
DatePeriod(years, months, days)
@@ -143,7 +143,7 @@ object DatePeriodISO8601Serializer: KSerializer<DatePeriod> {
143143
override fun deserialize(decoder: Decoder): DatePeriod =
144144
when (val period = DateTimePeriod.parse(decoder.decodeString())) {
145145
is DatePeriod -> period
146-
else -> throw IllegalArgumentException("$period is not a date-based period")
146+
else -> throw SerializationException("$period is not a date-based period")
147147
}
148148

149149
override fun serialize(encoder: Encoder, value: DatePeriod) {

core/common/src/serializers/InstantSerializers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object InstantComponentSerializer: KSerializer<Instant> {
4242
0 -> epochSeconds = decodeLongElement(descriptor, 0)
4343
1 -> nanosecondsOfSecond = decodeIntElement(descriptor, 1)
4444
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
45-
else -> error("Unexpected index: $index")
45+
else -> throw SerializationException("Unexpected index: $index")
4646
}
4747
}
4848
if (epochSeconds == null) throw MissingFieldException("epochSeconds")

core/common/src/serializers/LocalDateSerializers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object LocalDateComponentSerializer: KSerializer<LocalDate> {
4545
1 -> month = decodeShortElement(descriptor, 1)
4646
2 -> day = decodeShortElement(descriptor, 2)
4747
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
48-
else -> error("Unexpected index: $index")
48+
else -> throw SerializationException("Unexpected index: $index")
4949
}
5050
}
5151
if (year == null) throw MissingFieldException("year")

core/common/src/serializers/LocalDateTimeSerializers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object LocalDateTimeComponentSerializer: KSerializer<LocalDateTime> {
5757
5 -> second = decodeShortElement(descriptor, 5)
5858
6 -> nanosecond = decodeIntElement(descriptor, 6)
5959
CompositeDecoder.DECODE_DONE -> break@loop // https://youtrack.jetbrains.com/issue/KT-42262
60-
else -> error("Unexpected index: $index")
60+
else -> throw SerializationException("Unexpected index: $index")
6161
}
6262
}
6363
if (year == null) throw MissingFieldException("year")

core/common/src/serializers/TimeZoneSerializers.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import kotlinx.serialization.encoding.*
1313

1414
object TimeZoneSerializer: KSerializer<TimeZone> {
1515

16-
override val descriptor: SerialDescriptor
17-
get() = PrimitiveSerialDescriptor("TimeZone", PrimitiveKind.STRING)
16+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("TimeZone", PrimitiveKind.STRING)
1817

1918
override fun deserialize(decoder: Decoder): TimeZone = TimeZone.of(decoder.decodeString())
2019

@@ -26,8 +25,7 @@ object TimeZoneSerializer: KSerializer<TimeZone> {
2625

2726
object ZoneOffsetSerializer: KSerializer<ZoneOffset> {
2827

29-
override val descriptor: SerialDescriptor
30-
get() = PrimitiveSerialDescriptor("ZoneOffset", PrimitiveKind.STRING)
28+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ZoneOffset", PrimitiveKind.STRING)
3129

3230
override fun deserialize(decoder: Decoder): ZoneOffset {
3331
val zone = TimeZone.of(decoder.decodeString())

core/js/src/serializers/LocalDateTimeSerializers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ actual object LocalDateTimeCompactSerializer: KSerializer<LocalDateTime> {
3030
0 -> epochDay = decodeLongElement(descriptor, 0)
3131
1 -> nanoOfDay = decodeLongElement(descriptor, 1)
3232
CompositeDecoder.DECODE_DONE -> break
33-
else -> error("Unexpected index: $index")
33+
else -> throw SerializationException("Unexpected index: $index")
3434
}
3535
}
3636
if (epochDay == null) throw MissingFieldException("epochDay")

core/jvm/src/serializers/LocalDateTimeSerializers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ actual object LocalDateTimeCompactSerializer: KSerializer<LocalDateTime> {
3030
0 -> epochDay = decodeLongElement(descriptor, 0)
3131
1 -> nanoOfDay = decodeLongElement(descriptor, 1)
3232
CompositeDecoder.DECODE_DONE -> break
33-
else -> error("Unexpected index: $index")
33+
else -> throw SerializationException("Unexpected index: $index")
3434
}
3535
}
3636
if (epochDay == null) throw MissingFieldException("epochDay")

core/native/src/LocalDate.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public actual class LocalDate actual constructor(actual val year: Int, actual va
6666
internal fun ofEpochDay(epochDay: Int): LocalDate {
6767
// LocalDate(-999999, 1, 1).toEpochDay(), LocalDate(999999, 12, 31).toEpochDay()
6868
// Unidiomatic code due to https://github.com/Kotlin/kotlinx-datetime/issues/5
69-
require(epochDay >= -365961662 && epochDay <= 364522971) {
69+
require(epochDay >= MIN_EPOCH_DAY && epochDay <= MAX_EPOCH_DAY) {
7070
"Invalid date: boundaries of LocalDate exceeded"
7171
}
7272
var zeroDay = epochDay + DAYS_0000_TO_1970
@@ -100,6 +100,9 @@ public actual class LocalDate actual constructor(actual val year: Int, actual va
100100

101101
internal actual val MIN = LocalDate(YEAR_MIN, 1, 1)
102102
internal actual val MAX = LocalDate(YEAR_MAX, 12, 31)
103+
104+
internal const val MIN_EPOCH_DAY = -365961662
105+
internal const val MAX_EPOCH_DAY = 364522971
103106
}
104107

105108
// org.threeten.bp.LocalDate#toEpochDay

core/native/src/serializers/LocalDateSerializers.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package kotlinx.datetime.serializers
77

88
import kotlinx.datetime.LocalDate
9-
import kotlinx.datetime.clampToInt
109
import kotlinx.serialization.*
1110
import kotlinx.serialization.descriptors.*
1211
import kotlinx.serialization.encoding.*
@@ -16,11 +15,18 @@ actual object LocalDateLongSerializer: KSerializer<LocalDate> {
1615
override val descriptor: SerialDescriptor =
1716
PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.LONG)
1817

19-
override fun deserialize(decoder: Decoder): LocalDate =
20-
LocalDate.ofEpochDay(decoder.decodeLong().clampToInt())
18+
override fun deserialize(decoder: Decoder): LocalDate = dateFromLongEpochDays(decoder.decodeLong())
2119

2220
override fun serialize(encoder: Encoder, value: LocalDate) {
2321
encoder.encodeLong(value.toEpochDay().toLong())
2422
}
2523

24+
internal inline fun dateFromLongEpochDays(epochDays: Long): LocalDate =
25+
if (epochDays <= LocalDate.MAX_EPOCH_DAY.toLong() && epochDays >= LocalDate.MIN_EPOCH_DAY.toLong()) {
26+
LocalDate.ofEpochDay(epochDays.toInt())
27+
} else {
28+
throw SerializationException(
29+
"The passed value exceeds the platform-specific boundaries of days representable in LocalDate")
30+
}
31+
2632
}

core/native/src/serializers/LocalDateTimeSerializers.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
package kotlinx.datetime.serializers
77

8-
import kotlinx.datetime.LocalDate
98
import kotlinx.datetime.LocalDateTime
109
import kotlinx.datetime.LocalTime
11-
import kotlinx.datetime.clampToInt
1210
import kotlinx.serialization.*
1311
import kotlinx.serialization.descriptors.*
1412
import kotlinx.serialization.encoding.*
@@ -31,12 +29,12 @@ actual object LocalDateTimeCompactSerializer: KSerializer<LocalDateTime> {
3129
0 -> epochDay = decodeLongElement(descriptor, 0)
3230
1 -> nanoOfDay = decodeLongElement(descriptor, 1)
3331
CompositeDecoder.DECODE_DONE -> break
34-
else -> error("Unexpected index: $index")
32+
else -> throw SerializationException("Unexpected index: $index")
3533
}
3634
}
3735
if (epochDay == null) throw MissingFieldException("epochDay")
3836
if (nanoOfDay == null) throw MissingFieldException("nanoOfDay")
39-
val date = LocalDate.ofEpochDay(epochDay.clampToInt())
37+
val date = LocalDateLongSerializer.dateFromLongEpochDays(epochDay)
4038
val time = LocalTime.ofNanoOfDay(nanoOfDay)
4139
LocalDateTime(date, time)
4240
}

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ group=org.jetbrains.kotlinx
44
version=0.2.0
55
versionSuffix=SNAPSHOT
66

7+
serializationVersion=1.1.0
8+
79
kotlin.mpp.enableGranularSourceSetsMetadata=true
810
kotlin.mpp.enableCompatibilityMetadataVariant=true
911
kotlin.js.compiler=both

0 commit comments

Comments
 (0)