{-# LANGUAGE TemplateHaskell #-}
module Data.Aeson.THReader where

import Data.Aeson
import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Syntax.Compat
import TH.RelativePaths

readJSONFromFile :: (FromJSON a, Lift a) => String -> SpliceQ a
readJSONFromFile :: forall a. (FromJSON a, Lift a) => String -> SpliceQ a
readJSONFromFile String
name = Q (TExp a) -> Splice Q a
forall a (m :: * -> *). m (TExp a) -> Splice m a
liftSplice (Q (TExp a) -> Splice Q a) -> Q (TExp a) -> Splice Q a
forall a b. (a -> b) -> a -> b
$ do
    -- TODO: remove th-compat when we drop support for GHC 8.10
    ByteString
contents <- String -> Q ByteString
qReadFileLBS String
name
    case ByteString -> Either String a
forall a. FromJSON a => ByteString -> Either String a
eitherDecode ByteString
contents of
        Left String
err  -> String -> Q (TExp a)
forall a. String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
err
        Right a
res -> Splice Q a -> Q (TExp a)
forall (m :: * -> *) a. Splice m a -> m (TExp a)
examineSplice [||a
res||]