{-# LANGUAGE RankNTypes #-}
module Pipes.Aeson.Unchecked
(
encode
, decode
, decoded
, decodeL
, decodedL
) where
import Control.Monad (liftM)
import qualified Data.Aeson as Ae
import qualified Data.Aeson.Parser as Ae (value')
import qualified Data.ByteString as B
import Pipes
import qualified Pipes.Aeson.Internal as I
import qualified Pipes.ByteString as PB
import qualified Pipes.Parse as Pipes
encode :: (Monad m, Ae.ToJSON a) => a -> Producer' B.ByteString m ()
encode = PB.fromLazy . Ae.encode
{-# RULES "p >-> for cat encode" forall p .
p >-> for cat encode = for p (\a -> PB.fromLazy (Ae.encode a))
#-}
decode
:: (Monad m, Ae.FromJSON a)
=> Pipes.Parser B.ByteString m ((Maybe (Either I.DecodingError a)))
decode = fmap (fmap snd) `liftM` decodeL
decodeL
:: (Monad m, Ae.FromJSON a)
=> Pipes.Parser B.ByteString m (Maybe (Either I.DecodingError (Int, a)))
decodeL = I.decodeL Ae.value'
decoded
:: (Monad m, Ae.FromJSON a, Ae.ToJSON a)
=> Lens' (Producer B.ByteString m r)
(Producer a m (Either (I.DecodingError, Producer B.ByteString m r) r))
decoded k p = fmap _encode (k (I.consecutively decode p))
where
_encode = \p0 -> do
er <- for p0 (\a -> encode a)
case er of
Left (_, p1) -> p1
Right r -> return r
{-# INLINE _encode #-}
decodedL
:: (Monad m, Ae.FromJSON a, Ae.ToJSON a)
=> Lens' (Producer B.ByteString m r)
(Producer (Int, a) m (Either (I.DecodingError, Producer B.ByteString m r) r))
decodedL k p = fmap _encode (k (I.consecutively decodeL p))
where
_encode = \p0 -> do
er <- for p0 (\(_, a) -> encode a)
case er of
Left (_, p1) -> p1
Right r -> return r
{-# INLINE _encode #-}
type Lens' s a = forall f . Functor f => (a -> f a) -> (s -> f s)