Among other thigs, it can generate boilerplate for you, so that you spend time on more important things.
Let’s say we have the following datatypes:
data Project
= Project
{ name :: String
resx :: Int
, resy :: Int
, _models :: [Model]
, _textures :: [Texture]
, _cameraP :: [Float]
,deriving Show
}
data Model
= Model
{ _path :: String
deriving Show
}
data Texture
= Texture
{ _path :: String
deriving Show }
If we wanted, we could write JSON interfaces for it like this:
instance FromJSON Project where
Object o) =
parseJSON (Project
<$> ((o .: "project") >>= (.: "name"))
<*> ((o .: "project") >>= (.: "resx"))
<*> ((o .: "project") >>= (.: "resy"))
<*> ((o .: "project") >>= (.: "models"))
<*> ((o .: "project") >>= (.: "textures"))
<*> ((o .: "project") >>= (.: "camera"))
= mzero
parseJSON _
instance FromJSON Model where
Object o) =
parseJSON (Model
<$> o .: "path"
= mzero
parseJSON _
instance FromJSON Texture where
Object o) =
parseJSON (Texture
<$> o .: "path"
= mzero parseJSON _
Thanks to TH, we can write like this instead:
'Project
deriveJSON defaultOptions '
'Model
deriveJSON defaultOptions '
'Texture deriveJSON defaultOptions '
Both are equivalent.
Cheers, Vlad.