aeson is a Haskell library for converting JSON data to and from typed Haskell values. It fits naturally into a type-safe HTTP/JSON API: define an application type, provide JSON instances, and use decode for input and encode for output. section 16.0 section 16.1.1
The central API uses two typeclasses and a JSON representation:
- FromJSON defines parsing, while ToJSON defines serialization. Generic can automatically derive implementations based on a datatype definition. section 16.4
Parsing a todo entry
A todo application can model an entry as `TodoEntry { entryId :: Maybe Int, text :: Text, checked :: Bool, rank :: Maybe Int }`. Its parser accepts an object, reads optional `entryId` and `rank` fields with `.:?`, and requires `text` and `checked` with `.:`. The parser can then become the `FromJSON` implementation: `instance J.FromJSON TodoEntry where parseJSON = todoEntryParser`. Missing optional fields become `Nothing`, while a missing required field produces an error.
