liveBook · Manning

Deserialization

As explained in Kotlin in Action, 2E

Deserialization is the process of reconstructing objects or other in-memory data from a serialized representation.

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.
CH12_F08_Isakova

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

How Kotlin in Action, 2E covers it

How deep it goes
Traces lexer, parser, object seeds, reflection-based constructor calls, type conversion, validation, and reflection caching.
Examples
Uses Person, Author, and Book JSON inputs, plus annotated property names and excluded optional values.
What it assumes
Builds from Kotlin classes and JSON examples, while later relying on reified generics and Kotlin reflection APIs.
What you can do after
Evaluate/understand how a Kotlin JSON deserializer parses input, converts values, and constructs objects through reflection.
How it connects
Connects deserialization with JSON, serialization, annotations, reflection, reified type parameters, constructors, and companion objects.

Also explained in

Compare how 10 books explain deserialization →

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.