Support Firestore Timestamps for all platforms
See original GitHub issueLibrary | Class | Member | Platforms |
---|---|---|---|
Firestore (iOS) com.google.firebase (Android) |
FIRTimestamp (iOS) Timestamp (Android) |
N/A | All platforms |
There doesn’t seem a way to reliably deserialise FIRTimestamp
or com.google.firebase.Timestamp
. Given this simple stub:
val o1 = NSMutableDictionary()
o1.setObject( FIRTimestamp(1, 2), forKey = "test" as NSString)
val o2 = decode(TestData.serializer(), o1)
print("@@@ $o2")
I’ve attempted to deserialise it with the decode
function in dev.gitlive:firebase-common
:
-
Decode as string
@Serializable data class TestData(val test: String)
-
Decode as data class
@Serializable data class TestData(val test: TestTimestamp) @Serializable data class TestTimestamp(val seconds: Long, val nanoseconds: Int)
(1) works, but the resulting String is coming from -[NSObject description]
, which is not guaranteed to be a stable string representation. The format might also differ on different platforms.
(2) does not work, since it crashes when attempting to cast the ObjC FIRTimestamp
object to a Map<*, *>
.
Presumably this is the cause: https://github.com/GitLiveApp/firebase-kotlin-sdk/blob/master/firebase-common/src/iosMain/kotlin/dev/gitlive/firebase/_decoders.kt#L14
Given that
- (AFAIK)
FIRTimestamp
(or its Android equivalent) is the only exception - how the decoder contract of
kotlinx.serialization
is designed
So it seems it is a reasonable option to ad a special path for FIRTimestamp
in the said code path, so as to enable (2).
I’d love to contribute on this, so please let me know if this makes sense.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:8 (6 by maintainers)
Probably should consider the kotlinx-datetime lib too.
I now have a working version tested on Android only so far https://github.com/litclimbing/firebase-kotlin-sdk/tree/add-timestamp
My approach was to create specialized encoder/decoders and provide dummy serializers for the special types. The encoder/decoder look for the special types in the encode/decodeSerializableValue functions and does the translation there.
I also removed all the positive infinity double stuff used as a flag for the server timestamps. The FieldValues are recognized by the encoder and passed through.
Currently, I’ve only added support for firestore. The database module will need its own support added.