Instruction 1

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Introduction to Serialization & Deserialization

Serialization and deserialization are fundamental concepts in the context of JSON parsing, especially when it comes to transferring data between a server and a client or handling data in modern Android apps.

Serialization

Serialization is the process of converting an object into a format that can easily be stored or transmitted and later reconstructed. In the context of JSON parsing, serialization refers to converting Java or Kotlin objects into a JSON string. This is particularly useful when you need to send data from your app to a server or save it in a format that’s easily readable and accessible. An example of this is converting a User object with properties like name and email into a JSON string that represents the same data.

data class User(
  val name: String,
  val email: String
)

val testUser = User(name = "Test", email = "mail@gmail.com")
{
  "name": "Test",
  "email": "mail@gmail.com"
}

Deserialization

Deserialization is the reverse process of serialization. It involves converting data from a stored or transmitted format back into an object. In JSON parsing, deserialization refers to converting a JSON string into a Java or Kotlin object. This is crucial when receiving JSON data from a server response or reading a JSON-formatted file, allowing the app to work with the data in a more natural, object-oriented way. The following diagram shows how serialization and deserialization are generally used in the context of Android apps.

Object Object Stream of Bytes Stream of Bytes Serialization Deserialization Database Memory File
Serialization & deserialization

Introducing Moshi

The Moshi library, designed specifically for Java and Kotlin, offers an efficient and straightforward way to handle both serialization and deserialization. It does so through the use of adapters that know how to convert the objects of a specific type to JSON and back. Here’s how Moshi facilitates these processes:

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo 1