liveBook · Manning

JKid

As explained in Kotlin in Action, 2E

JKid is a book-specific pure Kotlin library that serializes and deserializes objects as JSON using annotations and reflection.

JKid is a small pure Kotlin library that converts Kotlin objects to JSON and reconstructs objects from JSON. Reflection inspects arbitrary object properties at runtime, while annotations customize serialization and deserialization behavior. section 12.0 section 12.1.3

Serialize and deserialize a Person

Define a Kotlin data class, pass an instance to serialize, and receive JSON such as {"age": 29, "name": "Alice"}. Passing JSON to deserialize<Person> reconstructs Person(name=Alice, age=29). section 12.1.3

data class Person(val name: String, val age: Int)

fun main() {
   val person = Person("Alice", 29)
   println(serialize(person))
   // {"age": 29, "name": "Alice"}
}

A compact runnable example shows the basic serialization path from a Kotlin Person object to textual JSON. section 12.1.3

Annotations adapt the JSON representation without changing the Kotlin model:

  • @JsonName("alias") maps the firstName property to the JSON field alias during both serialization and deserialization. section 12.1.3
  • @JsonExclude omits the age property from JSON; a nullable property with a default value illustrates the customized model. section 12.1.3

How Kotlin in Action, 2E covers it

How deep it goes
Implements a readable library and examines its major parts, while stopping short of the feature breadth of production serializers.
Examples
Uses Person data classes, serialize and deserialize calls, JSON strings, and JsonName and JsonExclude annotations.
What it assumes
Assumes basic Kotlin classes and JSON usage while developing annotation processing and reflection through concrete library code.
What you can do after
Build it from scratch and inspect a small reflection-based Kotlin JSON library through implementation examples and exercises.
How it connects
Connects annotations and reflection with JSON serialization, deserialization, and Kotlin libraries such as kotlinx.serialization, Jackson, and Gson.

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.