MosaicSerializer

class MosaicSerializer(tileSerializers: Map<KClass<out TileSchema>, KSerializer<out TileSchema>> = emptyMap(), eventSerializers: Map<KClass<out EventSchema>, KSerializer<out EventSchema>> = emptyMap(), eventTriggerSerializers: Map<KClass<out EventTrigger>, KSerializer<out EventTrigger>> = emptyMap(), additionalSerializersModule: SerializersModule = EmptySerializersModule())

The polymorphic JSON serializer shared by mosaic-server and mosaic-client — the single source of truth for how a TileSchema/EventSchema/EventTrigger subtype resolves to and from JSON, and the mechanism that lets a wire payload carry a mix of built-in and third-party-registered types indistinguishably.

Every polymorphic hierarchy is registered by @SerialName (each schema's own class carries one), not by any custom discriminator logic — this class only decides which classes participate. The built-in catalog is always registered (via the private defaultTileSerializers/ defaultEventSerializers/defaultEventTriggerSerializers maps at the bottom of this file, one entry per shipped TileSchema/EventSchema/EventTrigger); the tileSerializers/ eventSerializers/eventTriggerSerializers constructor parameters add third-party ones on top — this is exactly what mosaic-client's MosaicModules builds from a consuming app's MosaicDependencyInjectionConfig.tileDefinitions/eventDefinitions/eventTriggerDefinition. A class present in both a default map and one of these parameters would collide; the framework never does this itself, since the built-in classes and any third-party ones are necessarily distinct types.

Beyond the top-level TileSchema/EventSchema/EventTrigger hierarchies, the json builder also registers every nested sealed polymorphic type a schema declares internally (e.g. TextFieldTileSchema.VisualTransformation, EvaluateDataEventSchema.Expression and its whole operation tree, AddTilesEventSchema.InsertionPosition, the various Where/Direction/FileType sealed types) — those aren't extensible by third parties the way the 3 top-level hierarchies are, since they're closed sealed types owned entirely by their enclosing built-in schema.

Json is configured with explicitNulls = false (an omitted field decodes as null rather than failing), encodeDefaults = true (default-valued fields are still written to the wire, so a client decoding an older/newer schema version still sees every field explicitly), and ignoreUnknownKeys = true (an unrecognized field is silently dropped rather than failing decode — the mechanism that keeps server and client tolerant of small version skew).

Parameters

tileSerializers

third-party TileSchema registrations, merged with the built-in catalog.

eventSerializers

third-party EventSchema registrations, merged with the built-in catalog.

eventTriggerSerializers

third-party EventTrigger registrations, merged with the built-in catalog.

additionalSerializersModule

extra polymorphic serializers unrelated to the 3 hierarchies above — for a field type that itself needs its own serializer registration, independent of tile/event/trigger registration.

Constructors

Link copied to clipboard
constructor(tileSerializers: Map<KClass<out TileSchema>, KSerializer<out TileSchema>> = emptyMap(), eventSerializers: Map<KClass<out EventSchema>, KSerializer<out EventSchema>> = emptyMap(), eventTriggerSerializers: Map<KClass<out EventTrigger>, KSerializer<out EventTrigger>> = emptyMap(), additionalSerializersModule: SerializersModule = EmptySerializersModule())

Properties

Link copied to clipboard
val json: Json

The configured Json instance every method on this class delegates to. Exposed directly (rather than kept private) for callers that need to hand it to a lower-level API expecting a plain kotlinx.serialization Json — e.g. Ktor's ContentNegotiation plugin (json(get<MosaicSerializer>().json)).

Functions

Link copied to clipboard
fun decodeEventFromJsonElement(jsonElement: JsonElement): EventSchema

Same as decodeTileFromJsonElement, for an EventSchema — the mechanism behind EventRunnerDataProcessor decoding a broadcast/pushed payload as an event to run inline, and MosaicRepository's own screen-payload decoding.

Link copied to clipboard
fun <T> decodeFromJsonElement(deserializer: DeserializationStrategy<T>, element: JsonElement): T

Decodes element as T using the given deserializer explicitly — the general escape hatch for a caller that already has its own DeserializationStrategy and a JsonElement rather than a raw string.

Link copied to clipboard
inline fun <T> decodeFromString(string: String): T

Decodes string as T, resolving T's serializer from this serializer's own serializersModule via reification.

fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T

Decodes string as T using the given deserializer explicitly.

Link copied to clipboard
fun decodeTileFromJsonElement(jsonElement: JsonElement): TileSchema

Decodes jsonElement as a TileSchema, resolving the concrete subtype polymorphically by its own @SerialName — the general entry point for decoding one tile out of a JSON tree whose concrete type isn't known ahead of time (e.g. one entry of a tiles array).

Link copied to clipboard
fun encodeEventToJsonElement(event: EventSchema): JsonElement

Same as encodeTileToJsonElement, for an EventSchema — used by EventHolder.update.

Link copied to clipboard
fun encodeTileToJsonElement(tile: TileSchema): JsonElement

Encodes tile to a JsonElement, resolving its serializer from its own runtime class — the mechanism TileHolder.update uses to encode a tile's current state before merging a JSON patch onto it, without the generic TileHolder<T> needing to know T's concrete serializer ahead of time.

Link copied to clipboard
fun <T> encodeToJsonElement(serializer: SerializationStrategy<T>, value: T): JsonElement

Encodes value to a JsonElement tree (not a string) using the given serializer.

Link copied to clipboard
inline fun <T> encodeToString(value: T): String

Encodes value to a JSON string, resolving T's serializer from this serializer's own serializersModule via reification — the convenient overload for a caller that has a compile-time type to encode, rather than an explicit SerializationStrategy.

fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String

Encodes value to a JSON string using the given serializer explicitly (rather than one resolved from T's reified type) — the general escape hatch for a caller that already has its own SerializationStrategy.

Link copied to clipboard
fun parseToJsonElement(string: String): JsonElement

Parses string into a raw JsonElement tree, without decoding it into any typed model — the first step before a polymorphic decode call that needs the tree to inspect/merge before committing to a concrete type.