summaryrefslogtreecommitdiff
path: root/src/Hakyll/Aeson.hs
blob: f4090d535f56ef435ae6c5b5a02388b0b5486827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Hakyll.Aeson
    ( compileJSON
    , jsonCompiler
    , compileYAML
    , yamlCompiler
    , JSONWriter (..)
    , YAMLWriter (..)
    ) where

import Prolog
import Hakyll
import Hakyll.Error
import Data.Aeson as A
import Data.Yaml qualified as Y


compileJSON :: (FromJSON a, StringConv s LazyByteString, Traversable t, MonadError [String] m) => t s -> m (t a)
compileJSON = traverse $ liftErr . A.eitherDecode . toSL

jsonCompiler :: FromJSON a => Compiler (Item a)
jsonCompiler = getResourceLBS >>= compileJSON

compileYAML :: (FromJSON a, StringConv s StrictByteString, Traversable t, MonadThrow m) => t s -> m (t a)
compileYAML = traverse $ Y.decodeThrow . toSL

yamlCompiler :: FromJSON a => Compiler (Item a)
yamlCompiler = getResourceLBS >>= compileYAML


newtype JSONWriter a = JSONWriter { runJSONWriter :: a }
    deriving stock (Generic, Show)
    deriving newtype (FromJSON, ToJSON, Binary)

instance ToJSON a => Writable (JSONWriter a) where
    write p = A.encodeFile p . runJSONWriter . itemBody

newtype YAMLWriter a = YAMLWriter { runYAMLWriter :: a }
    deriving stock (Generic, Show)
    deriving newtype (FromJSON, ToJSON, Binary)

instance ToJSON a => Writable (YAMLWriter a) where
    write p = Y.encodeFile p . runYAMLWriter . itemBody