diff --git a/lib/src/main/java/de/sipgate/federmappe/DefaultSerializersModule.kt b/lib/src/main/java/de/sipgate/federmappe/DefaultSerializersModule.kt new file mode 100644 index 0000000..c0a47dd --- /dev/null +++ b/lib/src/main/java/de/sipgate/federmappe/DefaultSerializersModule.kt @@ -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) +} diff --git a/lib/src/main/java/de/sipgate/federmappe/DocumentSnapshotExt.kt b/lib/src/main/java/de/sipgate/federmappe/DocumentSnapshotExt.kt index 10a3cb8..be3da6e 100644 --- a/lib/src/main/java/de/sipgate/federmappe/DocumentSnapshotExt.kt +++ b/lib/src/main/java/de/sipgate/federmappe/DocumentSnapshotExt.kt @@ -1,10 +1,13 @@ package de.sipgate.federmappe import com.google.firebase.firestore.DocumentSnapshot +import kotlinx.serialization.modules.SerializersModule -inline fun DocumentSnapshot.toObject(errorHandler: (Throwable) -> T? = { throw it }): T? = +inline fun DocumentSnapshot.toObject( + customSerializers: SerializersModule = DefaultSerializersModule, + errorHandler: (Throwable) -> T? = { throw it }): T? = try { - data?.toObjectWithSerializer() + data?.toObjectWithSerializer(customSerializers = customSerializers) } catch (ex: Throwable) { errorHandler(ex) } diff --git a/lib/src/main/java/de/sipgate/federmappe/QuerySnapshotExt.kt b/lib/src/main/java/de/sipgate/federmappe/QuerySnapshotExt.kt index 782f79c..c2d76e1 100644 --- a/lib/src/main/java/de/sipgate/federmappe/QuerySnapshotExt.kt +++ b/lib/src/main/java/de/sipgate/federmappe/QuerySnapshotExt.kt @@ -1,11 +1,14 @@ package de.sipgate.federmappe import com.google.firebase.firestore.QuerySnapshot +import kotlinx.serialization.modules.SerializersModule -inline fun QuerySnapshot.toObject(errorHandler: (Throwable) -> T? = { throw it }): List = +inline fun QuerySnapshot.toObject( + customSerializers: SerializersModule = DefaultSerializersModule, + errorHandler: (Throwable) -> T? = { throw it }): List = map { try { - it.data.toObjectWithSerializer() + it.data.toObjectWithSerializer(customSerializers = customSerializers) } catch (ex: Throwable) { errorHandler(ex) } diff --git a/lib/src/main/java/de/sipgate/federmappe/StringMapExt.kt b/lib/src/main/java/de/sipgate/federmappe/StringMapExt.kt index a14639b..41a032c 100644 --- a/lib/src/main/java/de/sipgate/federmappe/StringMapExt.kt +++ b/lib/src/main/java/de/sipgate/federmappe/StringMapExt.kt @@ -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 Map.toObjectWithSerializer( serializer: KSerializer = serializer(), - customSerializers: SerializersModule = SerializersModule { - contextual(DateSerializer) - contextual(UriSerializer) - } + customSerializers: SerializersModule ): T = serializer.deserialize( StringMapToObjectDecoder( this,