Skip to content

sol/aeson-qq

Repository files navigation

aeson-qq: JSON quasiquoter for Haskell

This package exposes the function aesonQQ that compile-time converts a string representation of a JSON value into a Data.Aeson.Value. aesonQQ has the signature

aesonQQ :: QuasiQuoter

and is used like

{-# LANGUAGE QuasiQuotes #-}
import Data.Aeson.QQ
import Data.Aeson (Value)

john :: Value
john = [aesonQQ| {age: 23, name: "John", likes: ["linux", "Haskell"]} |]

The quasiquoter can also interpolate variables like

jane :: Value
jane = [aesonQQ| {age: #{age}, name: #{name}} |]
  where
    age = 23 :: Int
    name = "Jane"

where the function toJSON. will be called on age and name at runtime.

You can also interpolate arbitrary Haskell expressions:

mary :: Value
mary = [aesonQQ| {age: #{succ age}, name: "Mary"} |]
  where
    age = 23 :: Int

If you want to replace the name of the key in a hash you'll use the $-syntax:

joe :: Value
joe = [aesonQQ| {$key: 23, name: "Joe"} |]
  where
    key = "age"