{-# LANGUAGE RankNTypes #-}
module Pipes.Aeson
(
encodeArray
, encodeObject
, decode
, decoded
, decodeL
, decodedL
, I.DecodingError(..)
) where
import Control.Monad (liftM)
import qualified Data.Aeson as Ae
import qualified Data.ByteString.Char8 as B
import Pipes
import qualified Pipes.Aeson.Internal as I
import qualified Pipes.Aeson.Unchecked as U
import qualified Pipes.Parse as Pipes
encodeObject :: Monad m => Ae.Object -> Producer' B.ByteString m ()
encodeObject = U.encode
{-# RULES "p >-> for cat encodeObject" forall p .
p >-> for cat encodeObject = for p encodeObject
#-}
encodeArray :: Monad m => Ae.Array -> Producer' B.ByteString m ()
encodeArray = U.encode
{-# RULES "p >-> for cat encodeArray" forall p .
p >-> for cat encodeArray = for p encodeArray
#-}
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.json'
decoded
:: (Monad m, Ae.FromJSON a, Ae.ToJSON a)
=> (Ae.Value -> Either Ae.Object Ae.Array)
-> Lens' (Producer B.ByteString m r)
(Producer a m (Either (I.DecodingError, Producer B.ByteString m r) r))
decoded f k p0 = fmap _encode (k (I.consecutively decode p0))
where
_encode = \p -> do
er <- for p (\a -> either encodeObject encodeArray (f (Ae.toJSON a)))
case er of
Left (_, p') -> p'
Right r -> return r
decodedL
:: (Monad m, Ae.FromJSON a, Ae.ToJSON a)
=> (Ae.Value -> Either Ae.Object Ae.Array)
-> Lens' (Producer B.ByteString m r)
(Producer (Int, a) m (Either (I.DecodingError, Producer B.ByteString m r) r))
decodedL f k p0 = fmap _encode (k (I.consecutively decode p0))
where
_encode = \p -> do
er <- for p (\(_, a) -> either encodeObject encodeArray (f (Ae.toJSON a)))
case er of
Left (_, p') -> p'
Right r -> return r
type Lens' s a = forall f . Functor f => (a -> f a) -> (s -> f s)