module Data.Aeson.TH.Smart
( deriveJSON
, deriveToJSON
, deriveFromJSON
, mkToJSON
, mkParseJSON
) where
import Data.Aeson ( toJSON, Object, object, (.=), (.:)
, ToJSON, toJSON
, FromJSON, parseJSON
)
import Data.Aeson.Types ( Value(..), Parser )
import Control.Applicative ( pure, (<$>), (<*>) )
import Control.Monad ( return, mapM, liftM2, fail )
import Data.Bool ( otherwise)
import Data.Default ( def, Default )
import Data.Eq ( (==) )
import Data.Function ( ($), (.), id )
import Data.Functor ( fmap )
import Data.List ( (++), foldl, foldl', intercalate
, length, map, zip, genericLength
)
import Data.Maybe ( Maybe(Nothing, Just) )
import Prelude ( String, (), Integer, fromIntegral, not, error, filter, fst, snd, Bool(..), flip, concat)
import Text.Printf ( printf )
import Text.Show ( show )
#if __GLASGOW_HASKELL__ < 700
import Control.Monad ( (>>=) )
import Prelude ( fromInteger )
#endif
import qualified Data.HashMap.Strict as H ( lookup, toList, size )
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import qualified Data.Text as T ( Text, pack, unpack )
import qualified Data.Vector as V ( unsafeIndex, null, length, create, filter)
import qualified Data.Vector.Mutable as VM ( unsafeNew, unsafeWrite )
deriveJSON :: (String -> String)
-> Name
-> Q [Dec]
deriveJSON withField name =
liftM2 (++)
(deriveToJSON withField name)
(deriveFromJSON withField name)
deriveToJSON :: (String -> String)
-> Name
-> Q [Dec]
deriveToJSON withField name =
withType name $ \tvbs cons -> fmap (:[]) $ fromCons tvbs cons
where
fromCons :: [TyVarBndr] -> [Con] -> Q Dec
fromCons tvbs cons =
instanceD (return $ map (\t -> ClassP ''ToJSON [VarT t]) typeNames)
(classType `appT` instanceType)
[ funD 'toJSON
[ clause []
(normalB $ consToJSON withField cons)
[]
]
]
where
classType = conT ''ToJSON
typeNames = map tvbName tvbs
instanceType = foldl' appT (conT name) $ map varT typeNames
mkToJSON :: (String -> String)
-> Name
-> Q Exp
mkToJSON withField name = withType name (\_ cons -> consToJSON withField cons)
consToJSON :: (String -> String)
-> [Con]
-> Q Exp
consToJSON _ [] = error $ "Data.Aeson.TH.consToJSON: "
++ "Not a single constructor given!"
consToJSON withField [con] = do
value <- newName "value"
lam1E (varP value)
$ caseE (varE value)
[encodeArgs Nothing withField con]
consToJSON withField cons = do
value <- newName "value"
lam1E (varP value)
$ caseE (varE value)
[ encodeArgs (Just $ wrap $ [|String . T.pack|] `appE` conNameExp con) withField con
| con <- cons
]
where
wrap :: Q Exp -> [Q Exp] -> Q Exp
wrap name exps =
[e|object|] `appE` ([e| filter (not .(==Null) . snd )|] `appE`
listE (infixApp (litE $ stringL "constructor") [e|(.=)|] name : exps))
encodeArgs :: Maybe ([Q Exp] -> Q Exp) -> (String -> String) -> Con -> Q Match
encodeArgs _ _ c@(NormalC conName []) =
match (conP conName []) (normalB $ [e|toJSON|] `appE` ([|T.pack|] `appE` conNameExp c)) []
encodeArgs wrapper _ (NormalC conName ts) = do
let len = length ts
args <- mapM newName ["arg" ++ show n | n <- [1..len]]
let js = case [[e|toJSON|] `appE` varE arg | arg <- args] of
[e] -> e
es -> do
mv <- newName "mv"
let newMV = bindS (varP mv)
([e|VM.unsafeNew|] `appE`
litE (integerL $ fromIntegral len))
stmts = [noBindS $
[e|VM.unsafeWrite|] `appE`
(varE mv) `appE`
litE (integerL ix) `appE` e | (ix, e) <- zip [(0::Integer)..] es]
ret = noBindS $ [e|return|] `appE` varE mv
fltr = [e| V.filter (not . (== Null))|]
[e|Array|] `appE` (fltr `appE` (varE 'V.create `appE` doE (newMV:stmts++[ret])))
let b = case wrapper of
Nothing -> js
(Just wrapper') -> wrapper' [infixApp (litE (stringL "value")) [e|(.=)|] js]
match (conP conName $ map varP args) (normalB b) []
encodeArgs withExp withField (RecC conName ts) = do
args <- mapM newName ["arg" ++ show n | (_, n) <- zip ts [1 :: Integer ..]]
let args' = map (([e|toJSON|] `appE`) . varE) args
let js = [ infixApp ([e|T.pack|] `appE` fieldNameExp withField field) [e|(.=)|] arg
| (arg, (field, _, _)) <- zip args' ts
]
let b = case withExp of
Nothing -> [e|object|] `appE` ([e| filter (not . (==Null) . snd) |] `appE` listE js)
(Just wrapper) -> wrapper js
match (conP conName $ map varP args) (normalB b) []
encodeArgs withExp _ (InfixC _ conName _) = do
al <- newName "argL"
ar <- newName "argR"
let l = listE [[e|toJSON|] `appE` varE a | a <- [al,ar]]
let b = case withExp of
Nothing -> [e|toJSON|] `appE` l
(Just wrapper) -> wrapper [infixApp (litE $ stringL "value") [e|(.=)|] l]
match (infixP (varP al) conName (varP ar)) (normalB b) []
encodeArgs withExp withField (ForallC _ _ con) =
encodeArgs withExp withField con
deriveFromJSON :: (String -> String)
-> Name
-> Q [Dec]
deriveFromJSON withField name =
withType name $ \tvbs cons -> fmap (:[]) $ fromCons tvbs cons
where
fromCons :: [TyVarBndr] -> [Con] -> Q Dec
fromCons tvbs cons =
instanceD (return $ map (\t -> ClassP ''FromJSON [VarT t]) typeNames)
(classType `appT` instanceType)
[ funD 'parseJSON
[ clause []
(normalB $ consFromJSON name withField cons)
[]
]
]
where
classType = conT ''FromJSON
typeNames = map tvbName tvbs
instanceType = foldl' appT (conT name) $ map varT typeNames
mkParseJSON :: (String -> String)
-> Name
-> Q Exp
mkParseJSON withField name =
withType name (\_ cons -> consFromJSON name withField cons)
consFromJSON :: Name
-> (String -> String)
-> [Con]
-> Q Exp
consFromJSON _ _ [] = error $ "Data.Aeson.TH.consFromJSON: "
++ "Not a single constructor given!"
consFromJSON tName withField [con] = do
value <- newName "value"
lam1E (varP value)
$ caseE (varE value)
(parseArgs tName withField False con)
consFromJSON tName withField cons = do
value <- newName "value"
lam1E (varP value)
$ caseE (varE value)
$ concat [parseArgs tName withField True con | con <- cons]
objectWrapper :: Name -> Name -> (Name -> Name -> ExpQ) -> [Q Match]
objectWrapper tName conName expr =
[ do obj <- newName "arg"
strcon <- newName "strcon"
val <- newName "val"
flip (match (conP 'Object [varP obj])) [] $ normalB $ doE [
bindS (varP strcon) ([e|(.: "constructor")|] `appE` (varE obj))
, bindS (varP val) ([e|(.: "value")|] `appE` (varE obj))
, guardConName conName val
, noBindS (expr conName val)]
, matchFailed tName conName "Object"
]
recWrapper :: Name -> Name -> (String -> String) -> Name -> [VarStrictType] -> [ExpQ]
recWrapper tName conName withField obj ts =
[ do
b <- isInstance ''Default [ty]
[|lookupField|]
`appE` (if b then [| Just def |] else [| Nothing|])
`appE` (litE $ stringL $ show tName)
`appE` (litE $ stringL $ nameBase conName)
`appE` (varE obj)
`appE` ( [e|T.pack|]
`appE`
fieldNameExp withField field
)
| (field, _, ty) <- ts]
parseArgs :: Name
-> (String -> String)
-> Bool
-> Con
-> [Q Match]
parseArgs tName _ _ (NormalC conName []) =
[ do str <- newName "str"
match (conP 'String [varP str])
( normalB $
caseE (varE str)
[ match (litP $ stringL $ nameBase conName) (normalB $ [e|return|] `appE` conE conName) []
, match wildP (normalB $ wrongEnumerationError tName str) []
]
) []
, matchFailed tName conName "String"
]
parseArgs _ _ False (NormalC conName [_]) =
[ do arg <- newName "arg"
match (varP arg)
( normalB $ infixApp (conE conName)
[e|(<$>)|]
([e|parseJSON|] `appE` varE arg)
)
[]
]
parseArgs tName _ True (NormalC conName [_]) =
objectWrapper tName conName (\con val-> infixApp (conE con) [e|(<$>)|] ([e|parseJSON|] `appE` varE val))
parseArgs tName _ False (NormalC conName ts) = parseProduct tName conName $ genericLength ts
parseArgs tName _ True (NormalC conName ts) =
objectWrapper tName conName (\con val-> caseE (varE val) (parseProduct tName con $ genericLength ts))
parseArgs tName withField False (RecC conName ts) =
[ do obj <- newName "recObj"
let x:xs = recWrapper tName conName withField obj ts
match (conP 'Object [varP obj])
( normalB $ ( foldl' (\a b -> infixApp a [|(<*>)|] b)
(infixApp (conE conName) [|(<$>)|] x)
xs
)
)
[]
, matchFailed tName conName "Object"
]
parseArgs tName withField True (RecC conName ts) =
[ do obj <- newName "recObj"
let xs = recWrapper tName conName withField obj ts
x = [|lookupField Nothing|]
`appE` (litE $ stringL $ show tName)
`appE` (litE $ stringL $ nameBase conName)
`appE` (varE obj)
`appE` (litE $ stringL "constructor")
match (conP 'Object [varP obj])
( normalB $ ( foldl' (\a b -> infixApp a [|(<*>)|] b)
(infixApp (conE conName) [|(<$>)|] x)
xs
)
)
[]
, matchFailed tName conName "Object"
]
parseArgs tName _ False (InfixC _ conName _) = parseProduct tName conName 2
parseArgs tName _ True (InfixC _ conName _) =
objectWrapper tName conName (\con val -> caseE (varE val) (parseProduct tName con 2))
parseArgs tName withField b (ForallC _ _ con) = parseArgs tName withField b con
parseProduct :: Name
-> Name
-> Integer
-> [Q Match]
parseProduct tName conName numArgs =
[ do arr <- newName "arr"
let x:xs = [ [|parseJSON|]
`appE`
infixApp (varE arr)
[|V.unsafeIndex|]
(litE $ integerL ix)
| ix <- [0 .. numArgs 1]
]
match (conP 'Array [varP arr])
(normalB $ condE ( infixApp ([|V.length|] `appE` varE arr)
[|(==)|]
(litE $ integerL numArgs)
)
( foldl' (\a b -> infixApp a [|(<*>)|] b)
(infixApp (conE conName) [|(<$>)|] x)
xs
)
( parseTypeMismatch tName conName
(litE $ stringL $ "Array of length " ++ show numArgs)
( infixApp (litE $ stringL $ "Array of length ")
[|(++)|]
([|show . V.length|] `appE` varE arr)
)
)
)
[]
, matchFailed tName conName "Array"
]
lookupField :: (FromJSON a) => Maybe a -> String -> String -> Object -> T.Text -> Parser a
lookupField d tName rec obj key =
case H.lookup key obj of
Nothing -> case d of
Nothing -> unknownFieldFail tName rec (T.unpack key)
Just x -> return x
Just v -> parseJSON v
matchFailed :: Name -> Name -> String -> MatchQ
matchFailed tName conName expected = do
other <- newName "other"
match (varP other)
( normalB $ parseTypeMismatch tName conName
(litE $ stringL expected)
([|valueConName|] `appE` varE other)
)
[]
parseTypeMismatch :: Name -> Name -> ExpQ -> ExpQ -> ExpQ
parseTypeMismatch tName conName expected actual =
foldl appE
[|parseTypeMismatch'|]
[ litE $ stringL $ nameBase conName
, litE $ stringL $ show tName
, expected
, actual
]
wrongEnumerationError :: Name -> Name -> ExpQ
wrongEnumerationError tName str =
[|wrongEnumerationError'|] `appE` (litE $ stringL $ nameBase tName) `appE` ([|T.unpack|] `appE` varE str)
unknownFieldFail :: String -> String -> String -> Parser fail
unknownFieldFail tName rec key =
fail $ printf "When parsing the record %s of type %s the key %s was not present."
rec tName key
noObjectFail :: String -> String -> Parser fail
noObjectFail t o =
fail $ printf "When parsing %s expected Object but got %s." t o
wrongPairCountFail :: String -> String -> Parser fail
wrongPairCountFail t n =
fail $ printf "When parsing %s expected an Object with a single name/value pair but got %s pairs."
t n
conNotFoundFail :: String -> [String] -> String -> Parser fail
conNotFoundFail t cs o =
fail $ printf "When parsing %s expected an Object with a name/value pair where the name is one of [%s], but got %s."
t (intercalate ", " cs) o
parseTypeMismatch' :: String -> String -> String -> String -> Parser fail
parseTypeMismatch' tName conName expected actual =
fail $ printf "When parsing the constructor %s of type %s expected %s but got %s."
conName tName expected actual
wrongEnumerationError' :: String -> String -> Parser fail
wrongEnumerationError' tName str = fail $ printf (str ++ " is not a data constructor for type " ++ tName)
withType :: Name
-> ([TyVarBndr] -> [Con] -> Q a)
-> Q a
withType name f = do
info <- reify name
case info of
TyConI dec ->
case dec of
DataD _ _ tvbs cons _ -> f tvbs cons
NewtypeD _ _ tvbs con _ -> f tvbs [con]
other -> error $ "Data.Aeson.TH.withType: Unsupported type: "
++ show other
_ -> error "Data.Aeson.TH.withType: I need the name of a type."
getConName :: Con -> Name
getConName (NormalC name _) = name
getConName (RecC name _) = name
getConName (InfixC _ name _) = name
getConName (ForallC _ _ con) = getConName con
guardConName :: Name -> Name -> Q Stmt
guardConName conName varName = noBindS (infixApp (litE $ stringL $ nameBase conName) [e|(==)|] (varE varName))
tvbName :: TyVarBndr -> Name
tvbName (PlainTV name ) = name
tvbName (KindedTV name _) = name
conNameExp :: Con -> Q Exp
conNameExp = litE . stringL . nameBase . getConName
fieldNameExp :: (String -> String)
-> Name
-> Q Exp
fieldNameExp f = litE . stringL . f . nameBase
valueConName :: Value -> String
valueConName (Object _) = "Object"
valueConName (Array _) = "Array"
valueConName (String _) = "String"
valueConName (Number _) = "Number"
valueConName (Bool _) = "Boolean"
valueConName Null = "Null"