FirebaseFormat

sealed class FirebaseFormat : SerialFormat(source)

The multiplatform counterpart of the class mappers inside the Firebase Android SDK: Firestore's and Realtime Database's CustomClassMapper and Cloud Functions' Serializer.

The Android SDK maps custom classes by reflection. Reflection is not available on every platform, so this format maps classes through their kotlinx.serialization serializers instead, while following the Android mappers' rules so that code written for the Android SDK needs as few changes as possible: usually just @Serializable on each model class.

A format converts between Kotlin values and the plain values that the platform's Firebase SDK accepts and returns: null, Boolean, String, numbers, lists, maps with String keys and the product's own value types (such as a Firestore GeoPoint), which are passed through unchanged. The lists and maps are the platform's: Kotlin Lists and Maps on Android, the JVM and Apple platforms, whose SDKs exchange them, and JS arrays and objects on JS and wasmJs, whose SDK exchanges those (a Kotlin List or Map is read there too). So what a format produces can be handed to the SDK, and what the SDK returns can be handed to a format, on every platform.

  • The property names are the serial names: the Kotlin property name unless @SerialName says otherwise, and a @Transient property is left out. (The Android SDK's reflective mappers derive names from getters instead, storing isActive as active; a class migrated from them names such properties with @SerialName.)

  • The value rules, and the exception types and messages when a value is rejected, are those of the Android mapper of each product: see Firestore, Database and Functions.

  • The mapping annotations are this package's IgnoreExtraProperties, ThrowOnExtraProperties and, for Firestore, ServerTimestamp and DocumentId, which kotlinx.serialization records on every platform when they are applied to the class or the property itself. (The Android SDK's annotations of the same names are the SDK's own classes on Android, which kotlinx.serialization cannot record, so the product modules deprecate them towards these.)

Where kotlinx.serialization can do more than the Android mapper, the format keeps the extra support rather than reproducing an Android failure that no working Android code depends on: generic classes, objects, value classes and sealed class hierarchies (stored with a type property holding the subclass's serial name).

Inheritors

Types

Link copied to clipboard
class Database(val exception: (message: String) -> <Error class: unknown class>, val serializersModule: SerializersModule = EmptySerializersModule()) : FirebaseFormat

Realtime Database, following com.google.firebase.database.core.utilities.encoding.CustomClassMapper.

Link copied to clipboard
class Firestore(val bindings: FirebaseFormat.Firestore.Bindings, val serializersModule: SerializersModule = EmptySerializersModule()) : FirebaseFormat

Firestore, following com.google.firebase.firestore.util.CustomClassMapper.

Link copied to clipboard
class Functions(val serializersModule: SerializersModule = EmptySerializersModule()) : FirebaseFormat

Cloud Functions, following com.google.firebase.functions.Serializer.

Properties

Link copied to clipboard
open override val serializersModule: SerializersModule

Functions

Link copied to clipboard
fun <T : Any> convertToCustomClass(value: Any?, type: KClass<T>, documentReference: Any? = null): T?

Decodes plain values to an instance of type, found by its serializer, as the Android SDK's CustomClassMapper.convertToCustomClass(Object, Class, DocumentReference) does; null stays null. Any and the product's own value types are returned as they are, like Object and a Timestamp on Android.

Link copied to clipboard
fun convertToPlainValue(value: Any?): Any?

Converts value to plain values by its runtime type, as the Android SDK's mapper does with the Object given to calls such as DocumentReference.set(Object): plain values are checked and copied, the product's own value types are passed through and any other object is encoded with the serializer of its class, which must be @Serializable. The type arguments of a generic class are erased at runtime, so such a value goes through encodeToPlainValue, whose serializer knows them.

Link copied to clipboard
fun <T> decodeFromPlainValue(deserializer: DeserializationStrategy<T>, value: Any?, documentReference: Any? = null): T

Decodes plain values to a T with deserializer, as the Android SDK's mapper does in calls such as DocumentSnapshot.toObject(Class). For Firestore, documentReference is the document that @DocumentId properties receive (its id for a String property); it is ignored by the other products.

Link copied to clipboard
inline fun <T> FirebaseFormat.decodeFromPlainValue(value: Any?, documentReference: Any? = null): T

Decodes plain values to a T with the serializer of its type, where Any stands for any plain value.

Link copied to clipboard
fun <T> encodeToPlainValue(serializer: SerializationStrategy<T>, value: T): Any?

Encodes value to plain values with serializer.

Link copied to clipboard
inline fun <T> FirebaseFormat.encodeToPlainValue(value: T): Any?

Encodes value to plain values with the serializer of its type, where Any stands for any plain value.

Link copied to clipboard
fun serializerFor(type: KType): KSerializer<Any?>

The serializer for type, where Any (as in Map<String, Any?>) stands for the plain value itself, as Object does for the Android mapper: it is decoded as is and encoded by its runtime type. The product's own value types are passed through the same way, and the type arguments of a generic class are resolved by this rule too. So is a type without a serializer of its own, such as the Comparable a map of mixed values infers, unless it is the base of a polymorphic hierarchy registered in the serializersModule.