module Data.Hermes
(
decodeEither
, Decoder(runDecoder)
, HermesEnv
, withHermesEnv
, withInputBuffer
, atKey
, atKeyOptional
, atKeyStrict
, atPointer
, bool
, char
, double
, int
, scientific
, string
, text
, list
, nullable
, objectAsKeyValues
, day
, month
, quarter
, timeOfDay
, timeZone
, localTime
, utcTime
, zonedTime
, HermesException(..)
, DocumentError(..)
, isNull
, withArray
, withBool
, withDocumentValue
, withDouble
, withInt
, withObject
, withString
, withText
, withRawByteString
, Array
, ArrayIter
, Document
, InputBuffer
, Object
, Parser
, Value
) where
import Control.Monad.Trans.Reader (ReaderT(..), runReaderT)
import Data.ByteString (ByteString)
import qualified System.IO.Unsafe as Unsafe
import UnliftIO.Exception (try)
import Data.Hermes.Decoder
import Data.Hermes.SIMDJSON.Types
import Data.Hermes.SIMDJSON.Wrapper (withInputBuffer)
decodeEither :: (Value -> Decoder a) -> ByteString -> Either HermesException a
decodeEither :: (Value -> Decoder a) -> ByteString -> Either HermesException a
decodeEither Value -> Decoder a
d ByteString
bs =
IO (Either HermesException a) -> Either HermesException a
forall a. IO a -> a
Unsafe.unsafePerformIO (IO (Either HermesException a) -> Either HermesException a)
-> ((HermesEnv -> IO a) -> IO (Either HermesException a))
-> (HermesEnv -> IO a)
-> Either HermesException a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> IO (Either HermesException a)
forall (m :: * -> *) e a.
(MonadUnliftIO m, Exception e) =>
m a -> m (Either e a)
try (IO a -> IO (Either HermesException a))
-> ((HermesEnv -> IO a) -> IO a)
-> (HermesEnv -> IO a)
-> IO (Either HermesException a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HermesEnv -> IO a) -> IO a
forall (m :: * -> *) a.
MonadUnliftIO m =>
(HermesEnv -> m a) -> m a
withHermesEnv ((HermesEnv -> IO a) -> Either HermesException a)
-> (HermesEnv -> IO a) -> Either HermesException a
forall a b. (a -> b) -> a -> b
$ \HermesEnv
hEnv ->
ByteString -> (InputBuffer -> IO a) -> IO a
forall a. ByteString -> (InputBuffer -> IO a) -> IO a
withInputBuffer ByteString
bs ((InputBuffer -> IO a) -> IO a) -> (InputBuffer -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \InputBuffer
input ->
(ReaderT HermesEnv IO a -> HermesEnv -> IO a)
-> HermesEnv -> ReaderT HermesEnv IO a -> IO a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT HermesEnv IO a -> HermesEnv -> IO a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT HermesEnv
hEnv (ReaderT HermesEnv IO a -> IO a)
-> (Decoder a -> ReaderT HermesEnv IO a) -> Decoder a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Decoder a -> ReaderT HermesEnv IO a
forall a. Decoder a -> ReaderT HermesEnv IO a
runDecoder (Decoder a -> IO a) -> Decoder a -> IO a
forall a b. (a -> b) -> a -> b
$ (Value -> Decoder a) -> InputBuffer -> Decoder a
forall a. (Value -> Decoder a) -> InputBuffer -> Decoder a
withDocumentValue Value -> Decoder a
d InputBuffer
input