48 JSON (De)serialization with circe

 

After reading this lesson, you will be able to:

  • Convert a data structure or Scala instance into JSON format.
  • Parse JSON data as Scala code.

You have learned about asynchronous computations in the previous unit. In this lesson, you are going to learn about working with JSON in Scala. Scala doesn’t offer JSON support natively, so you’ll use circe, a popular library to work with JSON in Scala. JSON stands for “JavaScript Object Notation”, and it is a commonly used lightweight format to exchange data that is straightforward for humans to read and for machines to parse. You’ll see how to represent data in JSON format: you can refer to this process as serialization. You’ll also discover the inverse operation of defining a Scala instance by parsing a JSON structure: people refer to this operation as deserialization. Figure 48.1 provides a visual comparison between the processes of JSON serialization and JSON deserialization.

Figure 48.1: The process of serialization allows you to represent a Scala instance in JSON format. Its inverse is deserialization, which allows you to parse data in JSON format into a Scala instance.

In the capstone, your quiz application’s HTTP API will send and receive data using the JSON format.

48.1  Project Setup

48.2  JSON Serialization: from Instance to JSON

48.3  JSON deserialization: from JSON to Instance

48.4  Summary

48.5  Answers to Quick Checks