-- | A helper function for creating fields whose Haskell representation is
-- 'Integral' and which are serialized as numbers.
integralField :: Q Type -> String -> Field
integralField typq fname =
  let (~->) = appT . appT arrowT  -- constructs an arrow type
      (~::) = sigE . varE         -- (f ~:: t) constructs (f :: t)
  in (simpleField fname typq)
      { fieldRead = Just $
        [| \_ -> liftM $('fromInteger ~:: (conT ''Integer ~-> typq))
                   . JSON.readJSON |]
      , fieldShow = Just $
          [| \c -> (JSON.showJSON
                    . $('toInteger ~:: (typq ~-> conT ''Integer))
                    $ c, []) |]
      }
integralField :: Q Type -> String -> Field
integralField typq fname =
  let (~->) = appT . appT arrowT  -- constructs an arrow type
      (~::) = sigE . varE         -- (f ~:: t) constructs (f :: t)
  in (simpleField fname typq)
      { fieldRead = Just $
        [| \_ -> liftM $('fromInteger ~:: (conT ''Integer ~-> typq))
                   . JSON.readJSON |]
      , fieldShow = Just $
          [| \c -> (JSON.showJSON
                    . $('toInteger ~:: (typq ~-> conT ''Integer))
                    $ c, []) |]
      }
integralField typq fname =
  let (~->) = appT . appT arrowT  -- constructs an arrow type
      (~::) = sigE . varE         -- (f ~:: t) constructs (f :: t)
  in (simpleField fname typq)
      { fieldRead = Just $
        [| \_ -> liftM $('fromInteger ~:: (conT ''Integer ~-> typq))
                   . JSON.readJSON |]
      , fieldShow = Just $
          [| \c -> (JSON.showJSON
                    . $('toInteger ~:: (typq ~-> conT ''Integer))
                    $ c, []) |]
      }