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























