liveBook · Manning

Serialization

As explained in Kotlin in Action, 2E

Serialization is the process of converting an object's state into a representation that can be stored or transmitted and later reconstructed.

Serialization converts object state into a representation that can be stored or transmitted and later reconstructed. Two practical Kotlin patterns are preserving view state with Serializable and producing JSON through reflection. section 4.1.4 section 12.0

Serializable view state in Kotlin

class Button : View { override fun getCurrentState(): State = ButtonState() override fun restoreState(state: State) { /* ... */ } class ButtonState : State { /* ... */ } } Here, ButtonState is nested rather than inner, so it does not implicitly retain a reference to Button. That avoids the Java failure where serializing ButtonState also reaches a non-serializable outer Button. section 4.1.4

A reflection-based JSON serializer can proceed incrementally:

  • Write each property name and serialize its value; primitives, strings, collections, and nested objects receive corresponding JSON forms. section 12.2.2
  • Customize default behavior with annotations, which can control how selected classes and properties are serialized or deserialized. section 12.0

How Kotlin in Action, 2E covers it

How deep it goes
Explains nested-class failure mechanics and implements reflective property traversal, while deferring broader annotation customization details.
Examples
Demonstrates serializable view state, Kotlin ButtonState nesting, and a reflective JSON serializer that emits property-based objects.
What it assumes
Assumes Kotlin and Java class-nesting conventions, interfaces, Serializable, reflection APIs, and basic JSON representations.
What you can do after
Build a reflective JSON serializer and avoid accidental outer-instance references when defining serializable nested state classes.
How it connects
Connects serialization with nested classes, inner classes, Serializable, reflection, annotations, JSON, and deserialization.

Also explained in

Compare how 24 books explain serialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.