{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
module Data.Aeson.Combinators.Decode (
Decoder(..)
, auto
, nullable
, list, vector
, hashMapLazy, hashMapStrict
, mapLazy, mapStrict
, jsonNull
, key
, at
, index
, indexes
, element
, path
, maybe
, either
, oneOf
, void
, unit, bool
, int, integer, int8, int16, int32, int64
, word, word8, word16, word32, word64
#if (MIN_VERSION_base(4,8,0))
, natural
#endif
, float, double
, scientific
, char, text, string
, uuid, version
, zonedTime, localTime, timeOfDay
, utcTime
, day
#if (MIN_VERSION_time_compat(1,9,2))
, dayOfWeek
#endif
, decode, decode'
, eitherDecode, eitherDecode'
, decodeStrict, decodeStrict'
, eitherDecodeStrict, eitherDecodeStrict'
, decodeFileStrict, decodeFileStrict'
, eitherDecodeFileStrict, eitherDecodeFileStrict'
, parseMaybe
, parseEither
) where
import Prelude hiding (either, fail, maybe)
import qualified Prelude (either)
import Control.Applicative
import Control.Monad hiding (void)
import Control.Monad.Fail (MonadFail (..))
import qualified Control.Monad.Fail as Fail
import Data.Aeson.Internal (JSONPath, JSONPathElement (..))
import qualified Data.Aeson.Internal as AI
import qualified Data.Aeson.Parser as Parser
import qualified Data.Aeson.Parser.Internal as ParserI
import Data.Aeson.Types hiding (parseEither, parseMaybe)
import qualified Data.Aeson.Types as ATypes
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as LB
import Data.List.NonEmpty (NonEmpty (..))
import Data.Text (Text)
import qualified Data.Vector as Vector
import Data.Int (Int16, Int32, Int64, Int8)
import Data.Time.Calendar (Day)
#if (MIN_VERSION_time_compat(1,9,2))
import Data.Time.Calendar.Compat (DayOfWeek)
#endif
import Data.Time.Clock (UTCTime)
import Data.Time.LocalTime (LocalTime, TimeOfDay, ZonedTime)
import Data.UUID.Types (UUID)
import Data.Vector (Vector, (!?))
import Data.Version (Version)
import Data.Void (Void)
import Data.Word (Word, Word16, Word32, Word64,
Word8)
#if (MIN_VERSION_base(4,8,0))
import GHC.Natural (Natural)
#endif
import qualified Data.HashMap.Lazy as HL
import qualified Data.HashMap.Strict as HS
import qualified Data.Map.Lazy as ML
import qualified Data.Map.Strict as MS
import Data.Scientific (Scientific)
import Data.Traversable (traverse)
newtype Decoder a =
Decoder (Value -> Parser a)
instance Functor Decoder where
fmap :: (a -> b) -> Decoder a -> Decoder b
fmap a -> b
f (Decoder Value -> Parser a
d) = (Value -> Parser b) -> Decoder b
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser b) -> Decoder b)
-> (Value -> Parser b) -> Decoder b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Parser a -> Parser b) -> (Value -> Parser a) -> Value -> Parser b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Parser a
d
{-# INLINE fmap #-}
instance Applicative Decoder where
pure :: a -> Decoder a
pure a
val = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser a) -> Decoder a)
-> (Value -> Parser a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \Value
_ -> a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
val
{-# INLINE pure #-}
(Decoder Value -> Parser (a -> b)
f') <*> :: Decoder (a -> b) -> Decoder a -> Decoder b
<*> (Decoder Value -> Parser a
d) = (Value -> Parser b) -> Decoder b
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser b) -> Decoder b)
-> (Value -> Parser b) -> Decoder b
forall a b. (a -> b) -> a -> b
$
\Value
val ->
(\a -> b
f -> (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Value -> Parser a
d Value
val)) ((a -> b) -> Parser b) -> Parser (a -> b) -> Parser b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Value -> Parser (a -> b)
f' Value
val
{-# INLINE (<*>) #-}
instance Monad Decoder where
return :: a -> Decoder a
return = a -> Decoder a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(Decoder Value -> Parser a
a) >>= :: Decoder a -> (a -> Decoder b) -> Decoder b
>>= a -> Decoder b
f = (Value -> Parser b) -> Decoder b
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser b) -> Decoder b)
-> (Value -> Parser b) -> Decoder b
forall a b. (a -> b) -> a -> b
$
\Value
val -> case (Value -> Parser a) -> Value -> Result a
forall a b. (a -> Parser b) -> a -> Result b
parse Value -> Parser a
a Value
val of
Success a
v -> let (Decoder Value -> Parser b
res) = a -> Decoder b
f a
v
in Value -> Parser b
res Value
val
Result a
_ -> Value -> Parser b
forall a. Value -> Parser a
unexpected Value
val
{-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
fail = Fail.fail
#endif
instance Alternative Decoder where
empty :: Decoder a
empty = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder Value -> Parser a
forall a. Value -> Parser a
unexpected
{-# INLINE empty #-}
Decoder Value -> Parser a
a <|> :: Decoder a -> Decoder a -> Decoder a
<|> Decoder Value -> Parser a
b = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser a) -> Decoder a)
-> (Value -> Parser a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \Value
v -> Value -> Parser a
a Value
v Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Value -> Parser a
b Value
v
{-# INLINE (<|>) #-}
instance MonadFail Decoder where
fail :: String -> Decoder a
fail String
s = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser a) -> Decoder a)
-> (Value -> Parser a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \Value
_ -> String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
s
{-# INLINE fail #-}
auto :: FromJSON a => Decoder a
auto :: Decoder a
auto = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder Value -> Parser a
forall a. FromJSON a => Value -> Parser a
parseJSON
{-# INLINE auto #-}
nullable :: Decoder a -> Decoder (Maybe a)
nullable :: Decoder a -> Decoder (Maybe a)
nullable (Decoder Value -> Parser a
d) = (Value -> Parser (Maybe a)) -> Decoder (Maybe a)
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser (Maybe a)) -> Decoder (Maybe a))
-> (Value -> Parser (Maybe a)) -> Decoder (Maybe a)
forall a b. (a -> b) -> a -> b
$ \case
Value
Null -> Maybe a -> Parser (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
Value
other -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> Parser a -> Parser (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser a
d Value
other
{-# INLINE nullable #-}
list :: Decoder a -> Decoder [a]
list :: Decoder a -> Decoder [a]
list (Decoder Value -> Parser a
d) = (Value -> Parser [a]) -> Decoder [a]
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser [a]) -> Decoder [a])
-> (Value -> Parser [a]) -> Decoder [a]
forall a b. (a -> b) -> a -> b
$
(Value -> Parser a) -> Value -> Parser [a]
forall a. (Value -> Parser a) -> Value -> Parser [a]
listParser Value -> Parser a
d
{-# INLINE list #-}
vector :: Decoder a -> Decoder (Vector a)
vector :: Decoder a -> Decoder (Vector a)
vector (Decoder Value -> Parser a
d) = (Value -> Parser (Vector a)) -> Decoder (Vector a)
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser (Vector a)) -> Decoder (Vector a))
-> (Value -> Parser (Vector a)) -> Decoder (Vector a)
forall a b. (a -> b) -> a -> b
$ \case
Array Array
v -> (Value -> Parser a) -> Array -> Parser (Vector a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Vector a -> m (Vector b)
Vector.mapM Value -> Parser a
d Array
v
Value
other -> String -> Value -> Parser (Vector a)
forall a. String -> Value -> Parser a
typeMismatch String
"Array" Value
other
{-# INLINE vector #-}
hashMapLazy :: Decoder a -> Decoder (HL.HashMap Text a)
hashMapLazy :: Decoder a -> Decoder (HashMap Text a)
hashMapLazy (Decoder Value -> Parser a
d) = (Value -> Parser (HashMap Text a)) -> Decoder (HashMap Text a)
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser (HashMap Text a)) -> Decoder (HashMap Text a))
-> (Value -> Parser (HashMap Text a)) -> Decoder (HashMap Text a)
forall a b. (a -> b) -> a -> b
$ \case
Object Object
xs -> (Value -> Parser a) -> Object -> Parser (HashMap Text a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value -> Parser a
d Object
xs
Value
val -> String -> Value -> Parser (HashMap Text a)
forall a. String -> Value -> Parser a
typeMismatch String
"Array" Value
val
{-# INLINE hashMapLazy #-}
hashMapStrict :: Decoder a -> Decoder (HS.HashMap Text a)
hashMapStrict :: Decoder a -> Decoder (HashMap Text a)
hashMapStrict (Decoder Value -> Parser a
d) = (Value -> Parser (HashMap Text a)) -> Decoder (HashMap Text a)
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser (HashMap Text a)) -> Decoder (HashMap Text a))
-> (Value -> Parser (HashMap Text a)) -> Decoder (HashMap Text a)
forall a b. (a -> b) -> a -> b
$ \case
Object Object
xs -> (Value -> Parser a) -> Object -> Parser (HashMap Text a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value -> Parser a
d Object
xs
Value
val -> String -> Value -> Parser (HashMap Text a)
forall a. String -> Value -> Parser a
typeMismatch String
"Array" Value
val
{-# INLINE hashMapStrict #-}
mapLazy :: Decoder a -> Decoder (ML.Map Text a)
mapLazy :: Decoder a -> Decoder (Map Text a)
mapLazy Decoder a
dec = [(Text, a)] -> Map Text a
forall k a. Ord k => [(k, a)] -> Map k a
ML.fromList ([(Text, a)] -> Map Text a)
-> (HashMap Text a -> [(Text, a)]) -> HashMap Text a -> Map Text a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Text a -> [(Text, a)]
forall k v. HashMap k v -> [(k, v)]
HL.toList (HashMap Text a -> Map Text a)
-> Decoder (HashMap Text a) -> Decoder (Map Text a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder a -> Decoder (HashMap Text a)
forall a. Decoder a -> Decoder (HashMap Text a)
hashMapLazy Decoder a
dec
{-# INLINE mapLazy #-}
mapStrict :: Decoder a -> Decoder (MS.Map Text a)
mapStrict :: Decoder a -> Decoder (Map Text a)
mapStrict Decoder a
dec = [(Text, a)] -> Map Text a
forall k a. Ord k => [(k, a)] -> Map k a
MS.fromList ([(Text, a)] -> Map Text a)
-> (HashMap Text a -> [(Text, a)]) -> HashMap Text a -> Map Text a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap Text a -> [(Text, a)]
forall k v. HashMap k v -> [(k, v)]
HL.toList (HashMap Text a -> Map Text a)
-> Decoder (HashMap Text a) -> Decoder (Map Text a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder a -> Decoder (HashMap Text a)
forall a. Decoder a -> Decoder (HashMap Text a)
hashMapLazy Decoder a
dec
{-# INLINE mapStrict #-}
jsonNull :: a -> Decoder a
jsonNull :: a -> Decoder a
jsonNull a
a = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser a) -> Decoder a)
-> (Value -> Parser a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \case
Value
Null -> a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
Value
val -> String -> Value -> Parser a
forall a. String -> Value -> Parser a
typeMismatch String
"null" Value
val
{-# INLINE jsonNull #-}
key :: Text -> Decoder a -> Decoder a
key :: Text -> Decoder a -> Decoder a
key Text
t (Decoder Value -> Parser a
d) = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser a) -> Decoder a)
-> (Value -> Parser a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \case
Object Object
v -> Value -> Parser a
d (Value -> Parser a) -> Parser Value -> Parser a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Object
v Object -> Text -> Parser Value
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
t
Value
val -> String -> Value -> Parser a
forall a. String -> Value -> Parser a
typeMismatch String
"Object" Value
val
{-# INLINE key #-}
at :: [Text] -> Decoder a -> Decoder a
at :: [Text] -> Decoder a -> Decoder a
at [Text]
pth Decoder a
d = (Text -> Decoder a -> Decoder a)
-> Decoder a -> [Text] -> Decoder a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Text -> Decoder a -> Decoder a
forall a. Text -> Decoder a -> Decoder a
key Decoder a
d [Text]
pth
{-# INLINE at #-}
index :: Int -> Decoder a -> Decoder a
index :: Int -> Decoder a -> Decoder a
index Int
i (Decoder Value -> Parser a
d) = (Value -> Parser a) -> Decoder a
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser a) -> Decoder a)
-> (Value -> Parser a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \Value
val ->
case Value
val of
Array Array
vec -> case Array
vec Array -> Int -> Maybe Value
forall a. Vector a -> Int -> Maybe a
!? Int
i of
Just Value
v -> Value -> Parser a
d Value
v
Maybe Value
Nothing -> Value -> Parser a
forall a. Value -> Parser a
unexpected Value
val
Value
_ -> String -> Value -> Parser a
forall a. String -> Value -> Parser a
typeMismatch String
"Array" Value
val
{-# INLINE index #-}
indexes :: [Int] -> Decoder a -> Decoder a
indexes :: [Int] -> Decoder a -> Decoder a
indexes [Int]
pth Decoder a
d = (Int -> Decoder a -> Decoder a) -> Decoder a -> [Int] -> Decoder a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Int -> Decoder a -> Decoder a
forall a. Int -> Decoder a -> Decoder a
index Decoder a
d [Int]
pth
{-# INLINE indexes #-}
element :: JSONPathElement -> Decoder a -> Decoder a
element :: JSONPathElement -> Decoder a -> Decoder a
element (Key Text
txt) = Text -> Decoder a -> Decoder a
forall a. Text -> Decoder a -> Decoder a
key Text
txt
element (Index Int
i) = Int -> Decoder a -> Decoder a
forall a. Int -> Decoder a -> Decoder a
index Int
i
{-# INLINE element #-}
path :: JSONPath -> Decoder a -> Decoder a
path :: JSONPath -> Decoder a -> Decoder a
path JSONPath
pth Decoder a
d = (JSONPathElement -> Decoder a -> Decoder a)
-> Decoder a -> JSONPath -> Decoder a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr JSONPathElement -> Decoder a -> Decoder a
forall a. JSONPathElement -> Decoder a -> Decoder a
element Decoder a
d JSONPath
pth
{-# INLINE path #-}
maybe :: Decoder a -> Decoder (Maybe a)
maybe :: Decoder a -> Decoder (Maybe a)
maybe (Decoder Value -> Parser a
d) =
(Value -> Parser (Maybe a)) -> Decoder (Maybe a)
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser (Maybe a)) -> Decoder (Maybe a))
-> (Value -> Parser (Maybe a)) -> Decoder (Maybe a)
forall a b. (a -> b) -> a -> b
$ \Value
val ->
case (Value -> Parser a) -> Value -> Result a
forall a b. (a -> Parser b) -> a -> Result b
parse Value -> Parser a
d Value
val of
Success a
x -> Maybe a -> Parser (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
Error String
_ -> Maybe a -> Parser (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
{-# INLINE maybe #-}
either :: Decoder a -> Decoder (Either String a)
either :: Decoder a -> Decoder (Either String a)
either (Decoder Value -> Parser a
d) =
(Value -> Parser (Either String a)) -> Decoder (Either String a)
forall a. (Value -> Parser a) -> Decoder a
Decoder ((Value -> Parser (Either String a)) -> Decoder (Either String a))
-> (Value -> Parser (Either String a)) -> Decoder (Either String a)
forall a b. (a -> b) -> a -> b
$ \Value
val ->
case (Value -> Parser a) -> Value -> Result a
forall a b. (a -> Parser b) -> a -> Result b
parse Value -> Parser a
d Value
val of
Success a
x -> Either String a -> Parser (Either String a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either String a
forall a b. b -> Either a b
Right a
x)
Error String
err -> Either String a -> Parser (Either String a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Either String a
forall a b. a -> Either a b
Left String
err)
{-# INLINE either #-}
oneOf :: NonEmpty (Decoder a) -> Decoder a
oneOf :: NonEmpty (Decoder a) -> Decoder a
oneOf (Decoder a
first :| [Decoder a]
rest) =
(Decoder a -> Decoder a -> Decoder a)
-> Decoder a -> [Decoder a] -> Decoder a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Decoder a -> Decoder a -> Decoder a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) Decoder a
first [Decoder a]
rest
{-# INLINE oneOf #-}
void :: Decoder Void
void :: Decoder Void
void = Decoder Void
forall a. FromJSON a => Decoder a
auto
{-# INLINE void #-}
unit :: Decoder ()
unit :: Decoder ()
unit = Decoder ()
forall a. FromJSON a => Decoder a
auto
{-# INLINE unit #-}
bool :: Decoder Bool
bool :: Decoder Bool
bool = Decoder Bool
forall a. FromJSON a => Decoder a
auto
{-# INLINE bool #-}
int :: Decoder Int
int :: Decoder Int
int = Decoder Int
forall a. FromJSON a => Decoder a
auto
{-# INLINE int #-}
int8 :: Decoder Int8
int8 :: Decoder Int8
int8 = Decoder Int8
forall a. FromJSON a => Decoder a
auto
{-# INLINE int8 #-}
int16 :: Decoder Int16
int16 :: Decoder Int16
int16 = Decoder Int16
forall a. FromJSON a => Decoder a
auto
{-# INLINE int16 #-}
int32 :: Decoder Int32
int32 :: Decoder Int32
int32 = Decoder Int32
forall a. FromJSON a => Decoder a
auto
{-# INLINE int32 #-}
int64 :: Decoder Int64
int64 :: Decoder Int64
int64 = Decoder Int64
forall a. FromJSON a => Decoder a
auto
{-# INLINE int64 #-}
integer :: Decoder Integer
integer :: Decoder Integer
integer = Decoder Integer
forall a. FromJSON a => Decoder a
auto
{-# INLINE integer #-}
#if (MIN_VERSION_base(4,8,0))
natural :: Decoder Natural
natural :: Decoder Natural
natural = Decoder Natural
forall a. FromJSON a => Decoder a
auto
{-# INLINE natural #-}
#endif
word :: Decoder Word
word :: Decoder Word
word = Decoder Word
forall a. FromJSON a => Decoder a
auto
{-# INLINE word #-}
word8 :: Decoder Word8
word8 :: Decoder Word8
word8 = Decoder Word8
forall a. FromJSON a => Decoder a
auto
{-# INLINE word8 #-}
word16 :: Decoder Word16
word16 :: Decoder Word16
word16 = Decoder Word16
forall a. FromJSON a => Decoder a
auto
{-# INLINE word16 #-}
word32 :: Decoder Word32
word32 :: Decoder Word32
word32 = Decoder Word32
forall a. FromJSON a => Decoder a
auto
{-# INLINE word32 #-}
word64 :: Decoder Word64
word64 :: Decoder Word64
word64 = Decoder Word64
forall a. FromJSON a => Decoder a
auto
{-# INLINE word64 #-}
float :: Decoder Float
float :: Decoder Float
float = Decoder Float
forall a. FromJSON a => Decoder a
auto
{-# INLINE float #-}
double :: Decoder Double
double :: Decoder Double
double = Decoder Double
forall a. FromJSON a => Decoder a
auto
{-# INLINE double #-}
scientific :: Decoder Scientific
scientific :: Decoder Scientific
scientific = Decoder Scientific
forall a. FromJSON a => Decoder a
auto
{-# INLINE scientific #-}
char :: Decoder Char
char :: Decoder Char
char = Decoder Char
forall a. FromJSON a => Decoder a
auto
{-# INLINE char #-}
string :: Decoder String
string :: Decoder String
string = Decoder String
forall a. FromJSON a => Decoder a
auto
{-# INLINE string #-}
text :: Decoder Text
text :: Decoder Text
text = Decoder Text
forall a. FromJSON a => Decoder a
auto
{-# INLINE text #-}
uuid :: Decoder UUID
uuid :: Decoder UUID
uuid = Decoder UUID
forall a. FromJSON a => Decoder a
auto
{-# INLINE uuid #-}
version :: Decoder Version
version :: Decoder Version
version = Decoder Version
forall a. FromJSON a => Decoder a
auto
{-# INLINE version #-}
zonedTime :: Decoder ZonedTime
zonedTime :: Decoder ZonedTime
zonedTime = Decoder ZonedTime
forall a. FromJSON a => Decoder a
auto
{-# INLINE zonedTime #-}
localTime :: Decoder LocalTime
localTime :: Decoder LocalTime
localTime = Decoder LocalTime
forall a. FromJSON a => Decoder a
auto
{-# INLINE localTime #-}
timeOfDay :: Decoder TimeOfDay
timeOfDay :: Decoder TimeOfDay
timeOfDay = Decoder TimeOfDay
forall a. FromJSON a => Decoder a
auto
{-# INLINE timeOfDay #-}
utcTime :: Decoder UTCTime
utcTime :: Decoder UTCTime
utcTime = Decoder UTCTime
forall a. FromJSON a => Decoder a
auto
{-# INLINE utcTime #-}
day :: Decoder Day
day :: Decoder Day
day = Decoder Day
forall a. FromJSON a => Decoder a
auto
{-# INLINE day #-}
#if (MIN_VERSION_time_compat(1,9,2))
dayOfWeek :: Decoder DayOfWeek
dayOfWeek :: Decoder DayOfWeek
dayOfWeek = Decoder DayOfWeek
forall a. FromJSON a => Decoder a
auto
{-# INLINE dayOfWeek #-}
#endif
decode :: Decoder a -> LB.ByteString -> Maybe a
decode :: Decoder a -> ByteString -> Maybe a
decode (Decoder Value -> Parser a
d) =
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
Parser.decodeWith Parser Value
ParserI.jsonEOF ((Value -> Parser a) -> Value -> Result a
forall a b. (a -> Parser b) -> a -> Result b
parse Value -> Parser a
d)
{-# INLINE decode #-}
decode' :: Decoder a -> LB.ByteString -> Maybe a
decode' :: Decoder a -> ByteString -> Maybe a
decode' (Decoder Value -> Parser a
d) =
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
Parser.decodeWith Parser Value
ParserI.jsonEOF' ((Value -> Parser a) -> Value -> Result a
forall a b. (a -> Parser b) -> a -> Result b
parse Value -> Parser a
d)
{-# INLINE decode' #-}
eitherDecode :: Decoder a -> LB.ByteString -> Either String a
eitherDecode :: Decoder a -> ByteString -> Either String a
eitherDecode (Decoder Value -> Parser a
d) =
Either (JSONPath, String) a -> Either String a
forall a. Either (JSONPath, String) a -> Either String a
eitherFormatError (Either (JSONPath, String) a -> Either String a)
-> (ByteString -> Either (JSONPath, String) a)
-> ByteString
-> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
Parser.eitherDecodeWith Parser Value
ParserI.jsonEOF ((Value -> Parser a) -> Value -> IResult a
forall a b. (a -> Parser b) -> a -> IResult b
AI.iparse Value -> Parser a
d)
{-# INLINE eitherDecode #-}
eitherDecode' :: Decoder a -> LB.ByteString -> Either String a
eitherDecode' :: Decoder a -> ByteString -> Either String a
eitherDecode' (Decoder Value -> Parser a
d) =
Either (JSONPath, String) a -> Either String a
forall a. Either (JSONPath, String) a -> Either String a
eitherFormatError (Either (JSONPath, String) a -> Either String a)
-> (ByteString -> Either (JSONPath, String) a)
-> ByteString
-> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
Parser.eitherDecodeWith Parser Value
ParserI.jsonEOF' ((Value -> Parser a) -> Value -> IResult a
forall a b. (a -> Parser b) -> a -> IResult b
AI.iparse Value -> Parser a
d)
{-# INLINE eitherDecode' #-}
decodeStrict :: Decoder a -> B.ByteString -> Maybe a
decodeStrict :: Decoder a -> ByteString -> Maybe a
decodeStrict (Decoder Value -> Parser a
d) =
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
Parser.decodeStrictWith Parser Value
ParserI.jsonEOF ((Value -> Parser a) -> Value -> Result a
forall a b. (a -> Parser b) -> a -> Result b
parse Value -> Parser a
d)
{-# INLINE decodeStrict #-}
decodeStrict' :: Decoder a -> B.ByteString -> Maybe a
decodeStrict' :: Decoder a -> ByteString -> Maybe a
decodeStrict' (Decoder Value -> Parser a
d) =
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
Parser.decodeStrictWith Parser Value
ParserI.jsonEOF' ((Value -> Parser a) -> Value -> Result a
forall a b. (a -> Parser b) -> a -> Result b
parse Value -> Parser a
d)
{-# INLINE decodeStrict' #-}
eitherDecodeStrict :: Decoder a -> B.ByteString -> Either String a
eitherDecodeStrict :: Decoder a -> ByteString -> Either String a
eitherDecodeStrict (Decoder Value -> Parser a
d) =
Either (JSONPath, String) a -> Either String a
forall a. Either (JSONPath, String) a -> Either String a
eitherFormatError (Either (JSONPath, String) a -> Either String a)
-> (ByteString -> Either (JSONPath, String) a)
-> ByteString
-> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
Parser.eitherDecodeStrictWith Parser Value
ParserI.jsonEOF ((Value -> Parser a) -> Value -> IResult a
forall a b. (a -> Parser b) -> a -> IResult b
AI.iparse Value -> Parser a
d)
{-# INLINE eitherDecodeStrict #-}
eitherDecodeStrict' :: Decoder a -> B.ByteString -> Either String a
eitherDecodeStrict' :: Decoder a -> ByteString -> Either String a
eitherDecodeStrict' (Decoder Value -> Parser a
d) =
Either (JSONPath, String) a -> Either String a
forall a. Either (JSONPath, String) a -> Either String a
eitherFormatError (Either (JSONPath, String) a -> Either String a)
-> (ByteString -> Either (JSONPath, String) a)
-> ByteString
-> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, String) a
Parser.eitherDecodeStrictWith Parser Value
ParserI.jsonEOF' ((Value -> Parser a) -> Value -> IResult a
forall a b. (a -> Parser b) -> a -> IResult b
AI.iparse Value -> Parser a
d)
{-# INLINE eitherDecodeStrict' #-}
decodeFileStrict :: Decoder a -> FilePath -> IO (Maybe a)
decodeFileStrict :: Decoder a -> String -> IO (Maybe a)
decodeFileStrict Decoder a
dec =
(ByteString -> Maybe a) -> IO ByteString -> IO (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Decoder a -> ByteString -> Maybe a
forall a. Decoder a -> ByteString -> Maybe a
decodeStrict Decoder a
dec) (IO ByteString -> IO (Maybe a))
-> (String -> IO ByteString) -> String -> IO (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO ByteString
B.readFile
{-# INLINE decodeFileStrict #-}
decodeFileStrict' :: Decoder a -> FilePath -> IO (Maybe a)
decodeFileStrict' :: Decoder a -> String -> IO (Maybe a)
decodeFileStrict' Decoder a
dec =
(ByteString -> Maybe a) -> IO ByteString -> IO (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Decoder a -> ByteString -> Maybe a
forall a. Decoder a -> ByteString -> Maybe a
decodeStrict' Decoder a
dec) (IO ByteString -> IO (Maybe a))
-> (String -> IO ByteString) -> String -> IO (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO ByteString
B.readFile
{-# INLINE decodeFileStrict' #-}
eitherDecodeFileStrict :: Decoder a -> FilePath -> IO (Either String a)
eitherDecodeFileStrict :: Decoder a -> String -> IO (Either String a)
eitherDecodeFileStrict Decoder a
dec =
(ByteString -> Either String a)
-> IO ByteString -> IO (Either String a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Decoder a -> ByteString -> Either String a
forall a. Decoder a -> ByteString -> Either String a
eitherDecodeStrict Decoder a
dec) (IO ByteString -> IO (Either String a))
-> (String -> IO ByteString) -> String -> IO (Either String a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO ByteString
B.readFile
{-# INLINE eitherDecodeFileStrict #-}
eitherDecodeFileStrict' :: Decoder a -> FilePath -> IO (Either String a)
eitherDecodeFileStrict' :: Decoder a -> String -> IO (Either String a)
eitherDecodeFileStrict' Decoder a
dec =
(ByteString -> Either String a)
-> IO ByteString -> IO (Either String a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Decoder a -> ByteString -> Either String a
forall a. Decoder a -> ByteString -> Either String a
eitherDecodeStrict' Decoder a
dec) (IO ByteString -> IO (Either String a))
-> (String -> IO ByteString) -> String -> IO (Either String a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO ByteString
B.readFile
{-# INLINE eitherDecodeFileStrict' #-}
parseMaybe :: Decoder a -> Value -> Maybe a
parseMaybe :: Decoder a -> Value -> Maybe a
parseMaybe (Decoder Value -> Parser a
f) = (Value -> Parser a) -> Value -> Maybe a
forall a b. (a -> Parser b) -> a -> Maybe b
ATypes.parseMaybe Value -> Parser a
f
{-# INLINE parseMaybe #-}
parseEither :: Decoder a -> Value -> Either String a
parseEither :: Decoder a -> Value -> Either String a
parseEither (Decoder Value -> Parser a
f) = (Value -> Parser a) -> Value -> Either String a
forall a b. (a -> Parser b) -> a -> Either String b
ATypes.parseEither Value -> Parser a
f
{-# INLINE parseEither #-}
eitherFormatError :: Either (JSONPath, String) a -> Either String a
eitherFormatError :: Either (JSONPath, String) a -> Either String a
eitherFormatError = ((JSONPath, String) -> Either String a)
-> (a -> Either String a)
-> Either (JSONPath, String) a
-> Either String a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
Prelude.either (String -> Either String a
forall a b. a -> Either a b
Left (String -> Either String a)
-> ((JSONPath, String) -> String)
-> (JSONPath, String)
-> Either String a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (JSONPath -> String -> String) -> (JSONPath, String) -> String
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry JSONPath -> String -> String
AI.formatError) a -> Either String a
forall a b. b -> Either a b
Right
{-# INLINE eitherFormatError #-}
#if !(MIN_VERSION_aeson(1,4,3))
unexpected :: Value -> Parser a
unexpected actual = Fail.fail $ "unexpected " ++ typeOf actual
{-# INLINE unexpected #-}
typeOf :: Value -> String
typeOf v = case v of
Object _ -> "Object"
Array _ -> "Array"
String _ -> "String"
Number _ -> "Number"
Bool _ -> "Boolean"
Null -> "Null"
{-# INLINE typeOf #-}
#endif