Skip to content

Commit

Permalink
Provide the option of passing custom Serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Feb 13, 2024
1 parent 1ba3a48 commit 12f4ac0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.sipgate.federmappe

import de.sipgate.federmappe.serializers.DateSerializer
import de.sipgate.federmappe.serializers.UriSerializer
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.contextual

val DefaultSerializersModule = SerializersModule {
contextual(DateSerializer)
contextual(UriSerializer)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package de.sipgate.federmappe

import com.google.firebase.firestore.DocumentSnapshot
import kotlinx.serialization.modules.SerializersModule

inline fun <reified T : Any> DocumentSnapshot.toObject(errorHandler: (Throwable) -> T? = { throw it }): T? =
inline fun <reified T : Any> DocumentSnapshot.toObject(
customSerializers: SerializersModule = DefaultSerializersModule,
errorHandler: (Throwable) -> T? = { throw it }): T? =
try {
data?.toObjectWithSerializer<T>()
data?.toObjectWithSerializer<T>(customSerializers = customSerializers)
} catch (ex: Throwable) {
errorHandler(ex)
}
7 changes: 5 additions & 2 deletions lib/src/main/java/de/sipgate/federmappe/QuerySnapshotExt.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package de.sipgate.federmappe

import com.google.firebase.firestore.QuerySnapshot
import kotlinx.serialization.modules.SerializersModule

inline fun <reified T : Any> QuerySnapshot.toObject(errorHandler: (Throwable) -> T? = { throw it }): List<T?> =
inline fun <reified T : Any> QuerySnapshot.toObject(
customSerializers: SerializersModule = DefaultSerializersModule,
errorHandler: (Throwable) -> T? = { throw it }): List<T?> =
map {
try {
it.data.toObjectWithSerializer()
it.data.toObjectWithSerializer(customSerializers = customSerializers)
} catch (ex: Throwable) {
errorHandler(ex)
}
Expand Down
8 changes: 1 addition & 7 deletions lib/src/main/java/de/sipgate/federmappe/StringMapExt.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package de.sipgate.federmappe

import de.sipgate.federmappe.serializers.DateSerializer
import de.sipgate.federmappe.serializers.UriSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.contextual
import kotlinx.serialization.serializer

inline fun <reified T : Any> Map<String, Any>.toObjectWithSerializer(
serializer: KSerializer<T> = serializer<T>(),
customSerializers: SerializersModule = SerializersModule {
contextual(DateSerializer)
contextual(UriSerializer)
}
customSerializers: SerializersModule
): T = serializer.deserialize(
StringMapToObjectDecoder(
this,
Expand Down

0 comments on commit 12f4ac0

Please sign in to comment.