-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split legacy Firestore Timestamp serializer into legacy (java.util.Da…
…te) and new (kotlinx.datetime.Instant) one
- Loading branch information
Showing
6 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
common/src/main/kotlin/de/sipgate/federmappe/common/DefaultSerializersModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package de.sipgate.federmappe.common | ||
|
||
import de.sipgate.federmappe.common.serializers.TimestampSerializer | ||
import de.sipgate.federmappe.common.serializers.TimestampToDateSerializer | ||
import de.sipgate.federmappe.common.serializers.UriSerializer | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.contextual | ||
|
||
val DefaultSerializersModule = SerializersModule { | ||
contextual(TimestampSerializer) | ||
contextual(TimestampToDateSerializer) | ||
contextual(UriSerializer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
.../src/main/kotlin/de/sipgate/federmappe/common/serializers/TimestampToInstantSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package de.sipgate.federmappe.common.serializers | ||
|
||
import kotlin.properties.Delegates | ||
import kotlinx.datetime.Instant | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.descriptors.element | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.encoding.decodeStructure | ||
import kotlinx.serialization.encoding.encodeStructure | ||
|
||
object TimestampToInstantSerializer : KSerializer<Instant> { | ||
override val descriptor = buildClassSerialDescriptor(serialName = "FirebaseTimestamp") { | ||
element<Long>("seconds") | ||
element<Int>("nanoseconds") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: Instant) = encoder.encodeStructure(descriptor) { | ||
encodeLongElement(descriptor, 0, value.epochSeconds / 1000) | ||
encodeIntElement(descriptor, 1, value.nanosecondsOfSecond) | ||
} | ||
|
||
@ExperimentalSerializationApi | ||
override fun deserialize(decoder: Decoder): Instant { | ||
return decoder.decodeStructure(descriptor = descriptor) { | ||
if (decodeSequentially()) { | ||
val seconds = decodeLongElement(descriptor, 0) | ||
val nanoSeconds = decodeIntElement(descriptor, 1) | ||
Instant.fromEpochSeconds(seconds, nanoSeconds) | ||
} else { | ||
var seconds by Delegates.notNull<Long>() | ||
var nanoSeconds by Delegates.notNull<Int>() | ||
|
||
while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> seconds = decodeLongElement(descriptor, 0) | ||
1 -> nanoSeconds = decodeIntElement(descriptor, 1) | ||
CompositeDecoder.DECODE_DONE -> break | ||
else -> error("Unexpected index: $index") | ||
} | ||
} | ||
|
||
Instant.fromEpochSeconds(seconds, nanoSeconds) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters