|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.bson.codecs.kotlinx |
| 17 | + |
| 18 | +import java.time.ZoneOffset |
| 19 | +import kotlinx.datetime.Instant |
| 20 | +import kotlinx.datetime.LocalDate |
| 21 | +import kotlinx.datetime.LocalDateTime |
| 22 | +import kotlinx.datetime.LocalTime |
| 23 | +import kotlinx.datetime.TimeZone |
| 24 | +import kotlinx.datetime.UtcOffset |
| 25 | +import kotlinx.datetime.atDate |
| 26 | +import kotlinx.datetime.atStartOfDayIn |
| 27 | +import kotlinx.datetime.toInstant |
| 28 | +import kotlinx.datetime.toLocalDateTime |
| 29 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 30 | +import kotlinx.serialization.KSerializer |
| 31 | +import kotlinx.serialization.SerializationException |
| 32 | +import kotlinx.serialization.descriptors.PrimitiveKind |
| 33 | +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor |
| 34 | +import kotlinx.serialization.descriptors.SerialDescriptor |
| 35 | +import kotlinx.serialization.encoding.Decoder |
| 36 | +import kotlinx.serialization.encoding.Encoder |
| 37 | +import kotlinx.serialization.modules.SerializersModule |
| 38 | +import kotlinx.serialization.modules.plus |
| 39 | +import org.bson.BsonDateTime |
| 40 | +import org.bson.codecs.kotlinx.utils.SerializationModuleUtils.isClassAvailable |
| 41 | + |
| 42 | +/** |
| 43 | + * The default serializers module |
| 44 | + * |
| 45 | + * Handles: |
| 46 | + * - ObjectId serialization |
| 47 | + * - BsonValue serialization |
| 48 | + * - Instant serialization |
| 49 | + * - LocalDate serialization |
| 50 | + * - LocalDateTime serialization |
| 51 | + * - LocalTime serialization |
| 52 | + */ |
| 53 | +@ExperimentalSerializationApi |
| 54 | +public val dateTimeSerializersModule: SerializersModule by lazy { |
| 55 | + var module = SerializersModule {} |
| 56 | + if (isClassAvailable("kotlinx.datetime.Instant")) { |
| 57 | + module += |
| 58 | + InstantAsBsonDateTime.serializersModule + |
| 59 | + LocalDateAsBsonDateTime.serializersModule + |
| 60 | + LocalDateTimeAsBsonDateTime.serializersModule + |
| 61 | + LocalTimeAsBsonDateTime.serializersModule |
| 62 | + } |
| 63 | + module |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Instant KSerializer. |
| 68 | + * |
| 69 | + * Encodes and decodes `Instant` objects to and from `BsonDateTime`. Data is extracted via |
| 70 | + * [kotlinx.datetime.Instant.fromEpochMilliseconds] and stored to millisecond accuracy. |
| 71 | + * |
| 72 | + * @since 5.2 |
| 73 | + */ |
| 74 | +@ExperimentalSerializationApi |
| 75 | +public object InstantAsBsonDateTime : KSerializer<Instant> { |
| 76 | + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("InstantAsBsonDateTime", PrimitiveKind.STRING) |
| 77 | + |
| 78 | + override fun serialize(encoder: Encoder, value: Instant) { |
| 79 | + when (encoder) { |
| 80 | + is BsonEncoder -> encoder.encodeBsonValue(BsonDateTime(value.toEpochMilliseconds())) |
| 81 | + else -> throw SerializationException("Instant is not supported by ${encoder::class}") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + override fun deserialize(decoder: Decoder): Instant { |
| 86 | + return when (decoder) { |
| 87 | + is BsonDecoder -> Instant.fromEpochMilliseconds(decoder.decodeBsonValue().asDateTime().value) |
| 88 | + else -> throw SerializationException("Instant is not supported by ${decoder::class}") |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Suppress("UNCHECKED_CAST") |
| 93 | + public val serializersModule: SerializersModule = SerializersModule { |
| 94 | + contextual(Instant::class, InstantAsBsonDateTime as KSerializer<Instant>) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * LocalDate KSerializer. |
| 100 | + * |
| 101 | + * Encodes and decodes `LocalDate` objects to and from `BsonDateTime`. |
| 102 | + * |
| 103 | + * Converts the `LocalDate` values to and from `UTC`. |
| 104 | + * |
| 105 | + * @since 5.2 |
| 106 | + */ |
| 107 | +@ExperimentalSerializationApi |
| 108 | +public object LocalDateAsBsonDateTime : KSerializer<LocalDate> { |
| 109 | + override val descriptor: SerialDescriptor = |
| 110 | + PrimitiveSerialDescriptor("LocalDateAsBsonDateTime", PrimitiveKind.STRING) |
| 111 | + |
| 112 | + override fun serialize(encoder: Encoder, value: LocalDate) { |
| 113 | + when (encoder) { |
| 114 | + is BsonEncoder -> { |
| 115 | + val epochMillis = value.atStartOfDayIn(TimeZone.UTC).toEpochMilliseconds() |
| 116 | + encoder.encodeBsonValue(BsonDateTime(epochMillis)) |
| 117 | + } |
| 118 | + else -> throw SerializationException("LocalDate is not supported by ${encoder::class}") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + override fun deserialize(decoder: Decoder): LocalDate { |
| 123 | + return when (decoder) { |
| 124 | + is BsonDecoder -> |
| 125 | + Instant.fromEpochMilliseconds(decoder.decodeBsonValue().asDateTime().value) |
| 126 | + .toLocalDateTime(TimeZone.UTC) |
| 127 | + .date |
| 128 | + else -> throw SerializationException("LocalDate is not supported by ${decoder::class}") |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Suppress("UNCHECKED_CAST") |
| 133 | + public val serializersModule: SerializersModule = SerializersModule { |
| 134 | + contextual(LocalDate::class, LocalDateAsBsonDateTime as KSerializer<LocalDate>) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * LocalDateTime KSerializer. |
| 140 | + * |
| 141 | + * Encodes and decodes `LocalDateTime` objects to and from `BsonDateTime`. Data is stored to millisecond accuracy. |
| 142 | + * |
| 143 | + * Converts the `LocalDateTime` values to and from `UTC`. |
| 144 | + * |
| 145 | + * @since 5.2 |
| 146 | + */ |
| 147 | +@ExperimentalSerializationApi |
| 148 | +public object LocalDateTimeAsBsonDateTime : KSerializer<LocalDateTime> { |
| 149 | + override val descriptor: SerialDescriptor = |
| 150 | + PrimitiveSerialDescriptor("LocalDateTimeAsBsonDateTime", PrimitiveKind.STRING) |
| 151 | + |
| 152 | + override fun serialize(encoder: Encoder, value: LocalDateTime) { |
| 153 | + when (encoder) { |
| 154 | + is BsonEncoder -> { |
| 155 | + val epochMillis = value.toInstant(UtcOffset(ZoneOffset.UTC)).toEpochMilliseconds() |
| 156 | + encoder.encodeBsonValue(BsonDateTime(epochMillis)) |
| 157 | + } |
| 158 | + else -> throw SerializationException("LocalDateTime is not supported by ${encoder::class}") |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + override fun deserialize(decoder: Decoder): LocalDateTime { |
| 163 | + return when (decoder) { |
| 164 | + is BsonDecoder -> |
| 165 | + Instant.fromEpochMilliseconds(decoder.decodeBsonValue().asDateTime().value) |
| 166 | + .toLocalDateTime(TimeZone.UTC) |
| 167 | + else -> throw SerializationException("LocalDateTime is not supported by ${decoder::class}") |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Suppress("UNCHECKED_CAST") |
| 172 | + public val serializersModule: SerializersModule = SerializersModule { |
| 173 | + contextual(LocalDateTime::class, LocalDateTimeAsBsonDateTime as KSerializer<LocalDateTime>) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * LocalTime KSerializer. |
| 179 | + * |
| 180 | + * Encodes and decodes `LocalTime` objects to and from `BsonDateTime`. Data is stored to millisecond accuracy. |
| 181 | + * |
| 182 | + * Converts the `LocalTime` values to and from EpochDay at `UTC`. |
| 183 | + * |
| 184 | + * @since 5.2 |
| 185 | + */ |
| 186 | +@ExperimentalSerializationApi |
| 187 | +public object LocalTimeAsBsonDateTime : KSerializer<LocalTime> { |
| 188 | + override val descriptor: SerialDescriptor = |
| 189 | + PrimitiveSerialDescriptor("LocalTimeAsBsonDateTime", PrimitiveKind.STRING) |
| 190 | + |
| 191 | + override fun serialize(encoder: Encoder, value: LocalTime) { |
| 192 | + when (encoder) { |
| 193 | + is BsonEncoder -> { |
| 194 | + val epochMillis = |
| 195 | + value.atDate(LocalDate.fromEpochDays(0)).toInstant(UtcOffset(ZoneOffset.UTC)).toEpochMilliseconds() |
| 196 | + encoder.encodeBsonValue(BsonDateTime(epochMillis)) |
| 197 | + } |
| 198 | + else -> throw SerializationException("LocalTime is not supported by ${encoder::class}") |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + override fun deserialize(decoder: Decoder): LocalTime { |
| 203 | + return when (decoder) { |
| 204 | + is BsonDecoder -> |
| 205 | + Instant.fromEpochMilliseconds(decoder.decodeBsonValue().asDateTime().value) |
| 206 | + .toLocalDateTime(TimeZone.UTC) |
| 207 | + .time |
| 208 | + else -> throw SerializationException("LocalTime is not supported by ${decoder::class}") |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Suppress("UNCHECKED_CAST") |
| 213 | + public val serializersModule: SerializersModule = SerializersModule { |
| 214 | + contextual(LocalTime::class, LocalTimeAsBsonDateTime as KSerializer<LocalTime>) |
| 215 | + } |
| 216 | +} |
0 commit comments