Skip to content

Serializers

CmdrNorthpaw edited this page Apr 15, 2021 · 3 revisions

As well as having serializable types for different inventories, KInventory has serializers for all of the inventory types made serializable by default using a feature of Kotlinx.serialization called surrogate serialization. It also provides tools so that you can easily create your own surrogates.

What this means in practice is that, when you want to serialize an inventory with a serializable type, you can do that without needing to convert the inventory to its serializable form. For instance, if you wanted to serialize a SimpleInventory you don't need to do this:

val inventory = SimpleInventory(/*...*/)

val json = Json.encodeToString(inventory.serializable().serializer(), inventory.serializable())

Instead, you can shorten it to:

val inventory = SimpleInventory(/*...*/)

val json = Json.encodeToString(SimpleInventorySerializer, inventory)

If you want to learn how to make your own serializers, that's covered in Making your own SerializableInventory

Clone this wiki locally