# Simple Multiplatform RPC with Kotlin Serialization

DevFeed: [Simple Multiplatform RPC with Kotlin Serialization](<https://devfeed.tech/articles/simple-multiplatform-rpc-with-kotlin-serialization-20970.md>)

Original publisher: [Read original article](<https://jakewharton.com/simple-multiplatform-rpc-with-kotlin-serialization/>)

Published: 2020-04-15T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [multiplatform](<https://devfeed.tech/topics/multiplatform.md>), [Remote Procedure Call (RPC)](<https://devfeed.tech/topics/rpc.md>), [JSON](<https://devfeed.tech/topics/json.md>), [SDK](<https://devfeed.tech/topics/sdk.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Android](<https://devfeed.tech/topics/android.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [arrays](<https://devfeed.tech/tags/arrays.md>), [callback](<https://devfeed.tech/tags/callback.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [events](<https://devfeed.tech/tags/events.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [json](<https://devfeed.tech/tags/json.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [library](<https://devfeed.tech/tags/library.md>), [model](<https://devfeed.tech/tags/model.md>), [multiplatform](<https://devfeed.tech/tags/multiplatform.md>), [rpc](<https://devfeed.tech/tags/rpc.md>), [sdk](<https://devfeed.tech/tags/sdk.md>), [serialization](<https://devfeed.tech/tags/serialization.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This article explains replacing Moshi-based JSON exchange with kotlinx.serialization for a Kotlin multiplatform Android and Cast application. It shows how shared models can be serialized and deserialized across Kotlin and JavaScript, including parsing JavaScript objects, using custom serializers, and sending typed events to drive Cast display animations. It also introduces the problem of identifying polymorphic event types when the serialized JSON lacks type information.

## Source excerpt

I recently played a minor role in helping add Cast support to an Android app. Both the Android app and Cast display are written in Kotlin. The Android Cast SDK relays JSON strings to the JavaScript SDK which invokes your callback with the deserialized equivalent as a JS object. A multiplatform library holds the model objects so that they can be shared between Android and JS. class Game( val players: Array<Player> ) class Player( val name: String, val color: String, val scores: Array<Int> ) Moshi serializes the models to JSON in the Android app. val game = Game(arrayOf( Player("Jesse", "#ff0000", arrayOf(1, 2, 3)), Player("Matt", "#ff00ff", arrayOf(3, 0, 2)) )) val gameAdapter = moshi.adapter(Game::class.java) val gameJson = gameAdapter.toJson(game) // {"players":[{"name":"Jesse",...},{"name":"Matt",...}]} castSdk.send(gameJson) The Cast app receives the deserialized JS object and interprets it as being of the same type. castSdk.addCustomMessageListener { message -> val game = message.data.unsafeCast<Game>() ui.render(game) } This works but imposes some severe limitations. The model objects can only use collections available natively to JS which means Arrays instead of Lists. Custom serialization is also not supported because the JSON to JS object conversion was happening outside the library. It was clear this setup wasn't going to work long-term. Kotlin Serialization kotlinx.serialization is Kotlin's multiplatform, reflection-free, format-agnostic serialization library. Its compiler plugin generates code for types which are annotated as @Serializable. +@Serializable class Game( val players: Array<Player> ) +@Serializable class Player( val name: String, Updating the Android app requires specifying that we're using the JSON format and supplying a reference to the generated serializer. -val gameAdapter = moshi.adapter(Game::class.java) -val gameJson = gameAdapter.toJson(game) +val gameJson = Json.stringify(Game.serializer(), game) // {"players":[{"name":"Jesse",...},{"