Kotlin Serialization is a cross-platform and multi-format framework for data serialization—converting trees of objects to strings, byte arrays, or other serial representations and back. Kotlin Serialization fully supports and enforces the Kotlin type system, making sure only valid objects can be deserialized.
Kotlin Serialization is not just a library. It is a compiler plugin that is bundled with the Kotlin compiler distribution itself. Build configuration is explained in README.md. Once the project is set up, we can start serializing some classes.
Chapter 1. Basic Serialization (start reading here)
- Basics
- Serializable classes
- Backing fields are serialized
- Constructor properties requirement
- Data validation
- Optional properties
- Optional property initializer call
- Required properties
- Transient properties
- Defaults are not encoded by default
- Nullable properties
- Type safety is enforced
- Referenced objects
- No compression of repeated references
- Generic classes
- Serial field names
Chapter 2. Builtin Classes
Chapter 3. Serializers
- Introduction to serializers
- Custom serializers
- Primitive serializer
- Delegating serializers
- Composite serializer via surrogate
- Handwritten composite serializer
- Sequential decoding protocol (experimental)
- Serializing 3rd party classes
- Passing a serializer manually
- Specifying a serializer on a property
- Specifying a serializer for a particular type
- Specifying serializers for a file
- Specifying a serializer globally using a typealias
- Custom serializers for a generic type
- Format-specific serializers
- Simultaneous use of plugin-generated and custom serializers
- Contextual serialization
- Deriving external serializer for another Kotlin class (experimental)
Chapter 4. Polymorphism
- Closed polymorphism
- Open polymorphism
- Registered subclasses
- Serializing interfaces
- Property of an interface type
- Static parent type lookup for polymorphism
- Explicitly marking polymorphic class properties
- Registering multiple superclasses
- Polymorphism and generic classes
- Merging library serializers modules
- Default polymorphic type handler for deserialization
- Default polymorphic type handler for serialization
Chapter 5. JSON Features
- Json configuration
- Pretty printing
- Lenient parsing
- Ignoring unknown keys
- Ignoring unknown keys per class
- Alternative Json names
- Encoding defaults
- Explicit nulls
- Coercing input values
- Allowing structured map keys
- Allowing special floating-point values
- Class discriminator for polymorphism
- Class discriminator output mode
- Decoding enums in a case-insensitive manner
- Global naming strategy
- Base64
- Json elements
- Json transformations
Chapter 6. Alternative and custom formats (experimental)
Appendix A. Serialization and value classes (IR-only)