{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Aeson
(
decode
, decode'
, eitherDecode
, eitherDecode'
, encode
, encodeFile
, decodeStrict
, decodeFileStrict
, decodeStrict'
, decodeFileStrict'
, eitherDecodeStrict
, eitherDecodeFileStrict
, eitherDecodeStrict'
, eitherDecodeFileStrict'
, AesonException (..)
, throwDecode
, throwDecodeStrict
, throwDecode'
, throwDecodeStrict'
, Value(..)
, Encoding
, fromEncoding
, Array
, Object
, Key
, DotNetTime(..)
, FromJSON(..)
, Result(..)
, fromJSON
, ToJSON(..)
, KeyValue(..)
, (<?>)
, JSONPath
, ToJSONKey(..)
, ToJSONKeyFunction(..)
, FromJSONKey(..)
, FromJSONKeyFunction(..)
, GToJSONKey()
, genericToJSONKey
, GFromJSONKey()
, genericFromJSONKey
, FromJSON1(..)
, parseJSON1
, FromJSON2(..)
, parseJSON2
, ToJSON1(..)
, toJSON1
, toEncoding1
, ToJSON2(..)
, toJSON2
, toEncoding2
, GFromJSON
, FromArgs
, GToJSON
, GToEncoding
, GToJSON'
, ToArgs
, Zero
, One
, genericToJSON
, genericLiftToJSON
, genericToEncoding
, genericLiftToEncoding
, genericParseJSON
, genericLiftParseJSON
, Options
, defaultOptions
, fieldLabelModifier
, constructorTagModifier
, allNullaryToStringTag
, omitNothingFields
, sumEncoding
, unwrapUnaryRecords
, tagSingleConstructors
, rejectUnknownFields
, SumEncoding(..)
, camelTo2
, defaultTaggedObject
, JSONKeyOptions
, keyModifier
, defaultJSONKeyOptions
, withObject
, withText
, withArray
, withScientific
, withBool
, withEmbeddedJSON
, Series
, pairs
, foldable
, (.:)
, (.:?)
, (.:!)
, (.!=)
, object
, json
, json'
, parseIndexedJSON
) where
import Prelude.Compat
import Control.Exception (Exception (..))
import Control.Monad.Catch (MonadThrow (..))
import Data.Aeson.Types.FromJSON (ifromJSON, parseIndexedJSON)
import Data.Aeson.Encoding (encodingToLazyByteString)
import Data.Aeson.Parser.Internal (decodeWith, decodeStrictWith, eitherDecodeWith, eitherDecodeStrictWith, jsonEOF, json, jsonEOF', json')
import Data.Aeson.Types
import Data.Aeson.Types.Internal (formatError)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
encode :: (ToJSON a) => a -> L.ByteString
encode :: forall a. ToJSON a => a -> ByteString
encode = Encoding' Value -> ByteString
forall a. Encoding' a -> ByteString
encodingToLazyByteString (Encoding' Value -> ByteString)
-> (a -> Encoding' Value) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Encoding' Value
forall a. ToJSON a => a -> Encoding' Value
toEncoding
encodeFile :: (ToJSON a) => FilePath -> a -> IO ()
encodeFile :: forall a. ToJSON a => FilePath -> a -> IO ()
encodeFile FilePath
fp = FilePath -> ByteString -> IO ()
L.writeFile FilePath
fp (ByteString -> IO ()) -> (a -> ByteString) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. ToJSON a => a -> ByteString
encode
decode :: (FromJSON a) => L.ByteString -> Maybe a
decode :: forall a. FromJSON a => ByteString -> Maybe a
decode = Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
decodeWith Parser Value
jsonEOF Value -> Result a
forall a. FromJSON a => Value -> Result a
fromJSON
{-# INLINE decode #-}
decodeStrict :: (FromJSON a) => B.ByteString -> Maybe a
decodeStrict :: forall a. FromJSON a => ByteString -> Maybe a
decodeStrict = Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
decodeStrictWith Parser Value
jsonEOF Value -> Result a
forall a. FromJSON a => Value -> Result a
fromJSON
{-# INLINE decodeStrict #-}
decodeFileStrict :: (FromJSON a) => FilePath -> IO (Maybe a)
decodeFileStrict :: forall a. FromJSON a => FilePath -> IO (Maybe a)
decodeFileStrict = (ByteString -> Maybe a) -> IO ByteString -> IO (Maybe a)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Maybe a
forall a. FromJSON a => ByteString -> Maybe a
decodeStrict (IO ByteString -> IO (Maybe a))
-> (FilePath -> IO ByteString) -> FilePath -> IO (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ByteString
B.readFile
decode' :: (FromJSON a) => L.ByteString -> Maybe a
decode' :: forall a. FromJSON a => ByteString -> Maybe a
decode' = Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
decodeWith Parser Value
jsonEOF' Value -> Result a
forall a. FromJSON a => Value -> Result a
fromJSON
{-# INLINE decode' #-}
decodeStrict' :: (FromJSON a) => B.ByteString -> Maybe a
decodeStrict' :: forall a. FromJSON a => ByteString -> Maybe a
decodeStrict' = Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
forall a.
Parser Value -> (Value -> Result a) -> ByteString -> Maybe a
decodeStrictWith Parser Value
jsonEOF' Value -> Result a
forall a. FromJSON a => Value -> Result a
fromJSON
{-# INLINE decodeStrict' #-}
decodeFileStrict' :: (FromJSON a) => FilePath -> IO (Maybe a)
decodeFileStrict' :: forall a. FromJSON a => FilePath -> IO (Maybe a)
decodeFileStrict' = (ByteString -> Maybe a) -> IO ByteString -> IO (Maybe a)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Maybe a
forall a. FromJSON a => ByteString -> Maybe a
decodeStrict' (IO ByteString -> IO (Maybe a))
-> (FilePath -> IO ByteString) -> FilePath -> IO (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ByteString
B.readFile
eitherFormatError :: Either (JSONPath, String) a -> Either String a
eitherFormatError :: forall a. Either (JSONPath, FilePath) a -> Either FilePath a
eitherFormatError = ((JSONPath, FilePath) -> Either FilePath a)
-> (a -> Either FilePath a)
-> Either (JSONPath, FilePath) a
-> Either FilePath a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FilePath -> Either FilePath a
forall a b. a -> Either a b
Left (FilePath -> Either FilePath a)
-> ((JSONPath, FilePath) -> FilePath)
-> (JSONPath, FilePath)
-> Either FilePath a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (JSONPath -> FilePath -> FilePath)
-> (JSONPath, FilePath) -> FilePath
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry JSONPath -> FilePath -> FilePath
formatError) a -> Either FilePath a
forall a b. b -> Either a b
Right
{-# INLINE eitherFormatError #-}
eitherDecode :: (FromJSON a) => L.ByteString -> Either String a
eitherDecode :: forall a. FromJSON a => ByteString -> Either FilePath a
eitherDecode = Either (JSONPath, FilePath) a -> Either FilePath a
forall a. Either (JSONPath, FilePath) a -> Either FilePath a
eitherFormatError (Either (JSONPath, FilePath) a -> Either FilePath a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> Either FilePath a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeWith Parser Value
jsonEOF Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE eitherDecode #-}
eitherDecodeStrict :: (FromJSON a) => B.ByteString -> Either String a
eitherDecodeStrict :: forall a. FromJSON a => ByteString -> Either FilePath a
eitherDecodeStrict =
Either (JSONPath, FilePath) a -> Either FilePath a
forall a. Either (JSONPath, FilePath) a -> Either FilePath a
eitherFormatError (Either (JSONPath, FilePath) a -> Either FilePath a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> Either FilePath a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeStrictWith Parser Value
jsonEOF Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE eitherDecodeStrict #-}
eitherDecodeFileStrict :: (FromJSON a) => FilePath -> IO (Either String a)
eitherDecodeFileStrict :: forall a. FromJSON a => FilePath -> IO (Either FilePath a)
eitherDecodeFileStrict =
(ByteString -> Either FilePath a)
-> IO ByteString -> IO (Either FilePath a)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Either FilePath a
forall a. FromJSON a => ByteString -> Either FilePath a
eitherDecodeStrict (IO ByteString -> IO (Either FilePath a))
-> (FilePath -> IO ByteString)
-> FilePath
-> IO (Either FilePath a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ByteString
B.readFile
{-# INLINE eitherDecodeFileStrict #-}
eitherDecode' :: (FromJSON a) => L.ByteString -> Either String a
eitherDecode' :: forall a. FromJSON a => ByteString -> Either FilePath a
eitherDecode' = Either (JSONPath, FilePath) a -> Either FilePath a
forall a. Either (JSONPath, FilePath) a -> Either FilePath a
eitherFormatError (Either (JSONPath, FilePath) a -> Either FilePath a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> Either FilePath a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeWith Parser Value
jsonEOF' Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE eitherDecode' #-}
eitherDecodeStrict' :: (FromJSON a) => B.ByteString -> Either String a
eitherDecodeStrict' :: forall a. FromJSON a => ByteString -> Either FilePath a
eitherDecodeStrict' =
Either (JSONPath, FilePath) a -> Either FilePath a
forall a. Either (JSONPath, FilePath) a -> Either FilePath a
eitherFormatError (Either (JSONPath, FilePath) a -> Either FilePath a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> Either FilePath a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeStrictWith Parser Value
jsonEOF' Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE eitherDecodeStrict' #-}
eitherDecodeFileStrict' :: (FromJSON a) => FilePath -> IO (Either String a)
eitherDecodeFileStrict' :: forall a. FromJSON a => FilePath -> IO (Either FilePath a)
eitherDecodeFileStrict' =
(ByteString -> Either FilePath a)
-> IO ByteString -> IO (Either FilePath a)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Either FilePath a
forall a. FromJSON a => ByteString -> Either FilePath a
eitherDecodeStrict' (IO ByteString -> IO (Either FilePath a))
-> (FilePath -> IO ByteString)
-> FilePath
-> IO (Either FilePath a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ByteString
B.readFile
{-# INLINE eitherDecodeFileStrict' #-}
throwFormatError :: MonadThrow m => Either (JSONPath, String) a -> m a
throwFormatError :: forall (m :: * -> *) a.
MonadThrow m =>
Either (JSONPath, FilePath) a -> m a
throwFormatError = ((JSONPath, FilePath) -> m a)
-> (a -> m a) -> Either (JSONPath, FilePath) a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (AesonException -> m a
forall e a. Exception e => e -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (AesonException -> m a)
-> ((JSONPath, FilePath) -> AesonException)
-> (JSONPath, FilePath)
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> AesonException
AesonException (FilePath -> AesonException)
-> ((JSONPath, FilePath) -> FilePath)
-> (JSONPath, FilePath)
-> AesonException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (JSONPath -> FilePath -> FilePath)
-> (JSONPath, FilePath) -> FilePath
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry JSONPath -> FilePath -> FilePath
formatError) a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return
{-# INLINE throwFormatError #-}
throwDecode :: forall a m. (FromJSON a, MonadThrow m) => L.ByteString -> m a
throwDecode :: forall a (m :: * -> *).
(FromJSON a, MonadThrow m) =>
ByteString -> m a
throwDecode = Either (JSONPath, FilePath) a -> m a
forall (m :: * -> *) a.
MonadThrow m =>
Either (JSONPath, FilePath) a -> m a
throwFormatError (Either (JSONPath, FilePath) a -> m a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeWith Parser Value
jsonEOF Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE throwDecode #-}
throwDecodeStrict :: forall a m. (FromJSON a, MonadThrow m) => B.ByteString -> m a
throwDecodeStrict :: forall a (m :: * -> *).
(FromJSON a, MonadThrow m) =>
ByteString -> m a
throwDecodeStrict =
Either (JSONPath, FilePath) a -> m a
forall (m :: * -> *) a.
MonadThrow m =>
Either (JSONPath, FilePath) a -> m a
throwFormatError (Either (JSONPath, FilePath) a -> m a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeStrictWith Parser Value
jsonEOF Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE throwDecodeStrict #-}
throwDecode' :: forall a m. (FromJSON a, MonadThrow m) => L.ByteString -> m a
throwDecode' :: forall a (m :: * -> *).
(FromJSON a, MonadThrow m) =>
ByteString -> m a
throwDecode' = Either (JSONPath, FilePath) a -> m a
forall (m :: * -> *) a.
MonadThrow m =>
Either (JSONPath, FilePath) a -> m a
throwFormatError (Either (JSONPath, FilePath) a -> m a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeWith Parser Value
jsonEOF' Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE throwDecode' #-}
throwDecodeStrict' :: forall a m. (FromJSON a, MonadThrow m) => B.ByteString -> m a
throwDecodeStrict' :: forall a (m :: * -> *).
(FromJSON a, MonadThrow m) =>
ByteString -> m a
throwDecodeStrict' =
Either (JSONPath, FilePath) a -> m a
forall (m :: * -> *) a.
MonadThrow m =>
Either (JSONPath, FilePath) a -> m a
throwFormatError (Either (JSONPath, FilePath) a -> m a)
-> (ByteString -> Either (JSONPath, FilePath) a)
-> ByteString
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
forall a.
Parser Value
-> (Value -> IResult a)
-> ByteString
-> Either (JSONPath, FilePath) a
eitherDecodeStrictWith Parser Value
jsonEOF' Value -> IResult a
forall a. FromJSON a => Value -> IResult a
ifromJSON
{-# INLINE throwDecodeStrict' #-}
newtype AesonException = AesonException String
deriving (Int -> AesonException -> FilePath -> FilePath
[AesonException] -> FilePath -> FilePath
AesonException -> FilePath
(Int -> AesonException -> FilePath -> FilePath)
-> (AesonException -> FilePath)
-> ([AesonException] -> FilePath -> FilePath)
-> Show AesonException
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
$cshowsPrec :: Int -> AesonException -> FilePath -> FilePath
showsPrec :: Int -> AesonException -> FilePath -> FilePath
$cshow :: AesonException -> FilePath
show :: AesonException -> FilePath
$cshowList :: [AesonException] -> FilePath -> FilePath
showList :: [AesonException] -> FilePath -> FilePath
Show)
instance Exception AesonException where
displayException :: AesonException -> FilePath
displayException (AesonException FilePath
str) = FilePath
"aeson: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
str