{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE TypeApplications #-}
module Dep.Value.JSON (
fromBytes,
JSONValueDecodeError (..)
) where
import Data.Aeson qualified
import Data.ByteString
import Data.Typeable
import Dep.Value
import Control.Exception
fromBytes ::
forall v m e.
(
Typeable v,
Data.Aeson.FromJSON v,
Monad m
) =>
m ByteString ->
Value v m
fromBytes :: m ByteString -> Value v m
fromBytes m ByteString
action = m v -> Value v m
forall v (m :: * -> *). m v -> Value v m
Value do
ByteString
bytes <- m ByteString
action
case ByteString -> Either String v
forall a. FromJSON a => ByteString -> Either String a
Data.Aeson.eitherDecodeStrict' ByteString
bytes of
Left String
errMsg -> JSONValueDecodeError -> m v
forall a e. Exception e => e -> a
throw (TypeRep -> String -> JSONValueDecodeError
JSONValueDecodeError (Proxy v -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy v
forall k (t :: k). Proxy t
Proxy @v)) String
errMsg)
Right v
r -> v -> m v
forall (f :: * -> *) a. Applicative f => a -> f a
pure v
r
data JSONValueDecodeError = JSONValueDecodeError TypeRep String deriving (Int -> JSONValueDecodeError -> ShowS
[JSONValueDecodeError] -> ShowS
JSONValueDecodeError -> String
(Int -> JSONValueDecodeError -> ShowS)
-> (JSONValueDecodeError -> String)
-> ([JSONValueDecodeError] -> ShowS)
-> Show JSONValueDecodeError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [JSONValueDecodeError] -> ShowS
$cshowList :: [JSONValueDecodeError] -> ShowS
show :: JSONValueDecodeError -> String
$cshow :: JSONValueDecodeError -> String
showsPrec :: Int -> JSONValueDecodeError -> ShowS
$cshowsPrec :: Int -> JSONValueDecodeError -> ShowS
Show)
instance Exception JSONValueDecodeError