16 JSON and SQL

 

This chapter covers

  • Parsing and serializing JSON
  • Deriving typeclasses, using Generic
  • Using SQLite databases with our programs

In previous chapters, we have gotten to know many techniques and concepts in Haskell and built quite a few tools using them. Now, it is time to use everything we learned to realize a larger project. This and the following chapter function as a showcase for building an application with Haskell talking to a database and the network, providing a type-safe HTTP/JSON API. Additionally, we will automatically derive a client application for our API to safely interface with it. We will get to know a few new packages that will help us realize this.

This chapter starts with a discussion on working with JSON data using the aeson library and how to generically derive type classes. Then, we will dive into using SQLite in our programs and how to pass data to and from the database. We will use this knowledge in the following chapter to construct our application.

16.1 Encoding values as JSON

16.1.1 Aeson and JSON parsing

16.1.2 Serializing to JSON

16.2 Deriving type classes with Generic

16.3 Using a SQLite database

16.3.1 The basics of sqlite-simple

16.3.2 ToRow and FromRow

16.3.3 Defining actions for database access

Summary