git.iko.soy/rowtype-yoga/psfp
on master help sign in
22 lines 803 B history raw
1module Shared.Json (readJsonAff, readAff) where
3import Prelude
5import Control.Monad.Error.Class (throwError)
6import Data.Argonaut (jsonParser)
7import Data.Argonaut.Core (Json)
8import Data.Argonaut.Decode (decodeJson, printJsonDecodeError)
9import Data.Argonaut.Decode.Class (class DecodeJson)
10import Data.Either (Either, either)
11import Effect.Aff (Aff)
12import Effect.Class (liftEffect)
13import Effect.Exception (throw, error)
15readJsonAff ∷ ∀ a. DecodeJson a => String -> Aff a
16readJsonAff s = either (throwError <<< error) pure (jsonParser s) >>= readAff
18readAff ∷ ∀ a. DecodeJson a => Json -> Aff a
19readAff s = either (throwError <<< error <<< printJsonDecodeError) pure (decodeJson s)
21orThrow ∷ ∀ a s. Show s => Either s a -> Aff a
22orThrow = either (show >>> throw >>> liftEffect) pure