Deserialization reconstructs an in-memory object from a serialized representation. With JSON, it reverses serialization: JSON text becomes a Kotlin object. A compact API can make the target type explicit through a reified type parameter, returning a newly created instance. section 12.1.3 section 12.2.4
Deserialize a nested object
data class Author(val name: String) data class Book(val title: String, val author: Author) val json = """{"title": "Catch-22", "author": {"name": "J. Heller"}}""" val book = deserialize<Book>(json) // Book(title=Catch-22, author=Author(name=J. Heller)) The reified Book type tells deserialize which result to construct. Nested JSON objects become nested Kotlin objects rather than remaining raw text. section 12.2.4
A reflection-based deserializer does more than instantiate a class. It lexes JSON characters into tokens, parses tokens into JSON structure, and routes simple values, objects, and arrays to a deserialization component. That component gathers constructor arguments, converts or validates values by type, recursively spawns nested objects, and invokes the primary constructor with callBy(). section 12.2.4 section 12.2.5
Figure 12.8 The process of parsing JSON: first, the lexer takes the input text and divides it into tokens. Then, the parser processes the different semantic elements. Finally, the deserializer turns them into the final Kotlin objects.
Follow the three-stage path from JSON input through tokens and semantic elements to final Kotlin objects. section 12.2.4
Annotations and cached reflection metadata make reconstruction configurable and practical:
- @JsonName maps a Kotlin property to a different JSON field name, so firstName can read the alias field. section 12.1.3
- @JsonExclude can omit a property, while @CustomSerializer supplies custom value conversion for a property type. section 12.1.3 section 12.2.5
- ClassInfoCache stores constructor parameters, annotation data, serializers, and nested-class information once per class instead of rediscovering them for every JSON key. section 12.2.5
- Required non-null, non-optional constructor parameters can be checked before construction; type-specific serializers can reject an unexpected JSON value. section 12.2.5
For a simpler domain type, a companion object can keep a fromJSON factory beside the class. Kotlin permits Person.fromJSON(...) even when the function lives in the companion object; Java accesses an unnamed companion through Person.Companion.fromJSON(...). A companion can also implement a JSONFactory interface, allowing the class name itself to serve as a factory instance. section 4.4.3









