Haskell Implementation of the JSON-API specification
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
README.md Add relationships to resource payload Jul 9, 2016


Circle CI

Haskell Implementation of the JSON-API specification


From the specification itself:

If you’ve ever argued with your team about the way your JSON responses should be formatted, JSON API can be your anti-bikeshedding tool.

By following shared conventions, you can increase productivity, take advantage of generalized tooling, and focus on what matters: your application.

Clients built around JSON API are able to take advantage of its features around efficiently caching responses, sometimes eliminating network requests entirely.

All in all, API discoverability and other HATEOAS principles make JSON-API an attractive resource serialization option.

The specification

Find the specification here

Example usage

Let's start with an example User record:

data User = User
  { userId        :: Int
  , userFirstName :: String
  , userLastName  :: String
  } deriving (Eq, Show)

$(deriveJSON defaultOptions ''User)

From this, we can use the json-api package to produce a payload conformant to the JSON-API specification like so:

-- Builds the Document which will be serialized as our
-- web server's response payload
mkDocument :: User -> Links -> Document User Text Int
mkDocument usr links =
    (Singleton $ toResource usr)
    (Just links)

-- Helper function to convert a User into a resource object
-- This could be our canonical serialization function for a User in any
-- response payload
toResource :: User -> Resource User Text
toResource user =
  Resource resourceId resourceType user resourceLinks resourceMetaData
    resourceId       = ResourceId . pack . show . userId $ user
    resourceType     = ResourceType "User"
    resourceLinks    = Just $ userLinks user
    resourceMetaData = Nothing

-- helper function to build links for a User resource
userLinks :: User -> Links
userLinks user = toLinks [ ("self", selfLink) ]
    selfLink = toURL selfPath
    selfPath = "/users/" <> (show $ userId user)

When delivered as a response from a web server, for example, we get a payload that looks like this:


Neat! We can see that if we would like the full User data for the User with ID=1, we can query /users/1. Discoverability!

We can also see from the top-level links data that this particular payload originated from /users/1.

This is a very simple example to provide an introduction to the basic idea behind JSON-API and how to use this library. Check out these examples for more robust representations of resourceful payloads. Here, you'll start to see the more comprehensive benefits of a discoverable API.

Example Project

There is an example project illustrating how the library can be used in the context of a web server.


Module documentation can be found on Hackage