{-# 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 :: Object -> Producer' ByteString m ()
encodeObject = Object -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a.
(Monad m, ToJSON a) =>
a -> Producer' ByteString m ()
U.encode
{-# INLINABLE encodeObject #-}
{-# RULES "p >-> for cat encodeObject" forall p .
p >-> for cat encodeObject = for p encodeObject
#-}
encodeArray :: Monad m => Ae.Array -> Producer' B.ByteString m ()
encodeArray :: Array -> Producer' ByteString m ()
encodeArray = Array -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a.
(Monad m, ToJSON a) =>
a -> Producer' ByteString m ()
U.encode
{-# INLINABLE encodeArray #-}
{-# 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 :: Parser ByteString m (Maybe (Either DecodingError a))
decode = (Either DecodingError (Int, a) -> Either DecodingError a)
-> Maybe (Either DecodingError (Int, a))
-> Maybe (Either DecodingError a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Int, a) -> a)
-> Either DecodingError (Int, a) -> Either DecodingError a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, a) -> a
forall a b. (a, b) -> b
snd) (Maybe (Either DecodingError (Int, a))
-> Maybe (Either DecodingError a))
-> StateT
(Producer ByteString m x) m (Maybe (Either DecodingError (Int, a)))
-> StateT
(Producer ByteString m x) m (Maybe (Either DecodingError a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` StateT
(Producer ByteString m x) m (Maybe (Either DecodingError (Int, a)))
forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
Parser ByteString m (Maybe (Either DecodingError (Int, a)))
decodeL
{-# INLINABLE decode #-}
decodeL
:: (Monad m, Ae.FromJSON a)
=> Pipes.Parser B.ByteString m (Maybe (Either I.DecodingError (Int, a)))
decodeL :: Parser ByteString m (Maybe (Either DecodingError (Int, a)))
decodeL = Parser ByteString Value
-> Parser ByteString m (Maybe (Either DecodingError (Int, a)))
forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
Parser ByteString Value
-> Parser ByteString m (Maybe (Either DecodingError (Int, a)))
I.decodeL Parser ByteString Value
Ae.json'
{-# INLINABLE decodeL #-}
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 :: (Value -> Either Object Array)
-> Lens'
(Producer ByteString m r)
(Producer a m (Either (DecodingError, Producer ByteString m r) r))
decoded f :: Value -> Either Object Array
f k :: Producer a m (Either (DecodingError, Producer ByteString m r) r)
-> f (Producer
a m (Either (DecodingError, Producer ByteString m r) r))
k p0 :: Producer ByteString m r
p0 = (Producer a m (Either (DecodingError, Producer ByteString m r) r)
-> Producer ByteString m r)
-> f (Producer
a m (Either (DecodingError, Producer ByteString m r) r))
-> f (Producer ByteString m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer a m (Either (DecodingError, Producer ByteString m r) r)
-> Producer ByteString m r
forall x' x a b.
Proxy x' x () a m (Either (a, Proxy x' x () ByteString m b) b)
-> Proxy x' x () ByteString m b
_encode (Producer a m (Either (DecodingError, Producer ByteString m r) r)
-> f (Producer
a m (Either (DecodingError, Producer ByteString m r) r))
k (Parser ByteString m (Maybe (Either DecodingError a))
-> Producer ByteString m r
-> Producer a m (Either (DecodingError, Producer ByteString m r) r)
forall (m :: * -> *) e a r.
Monad m =>
Parser ByteString m (Maybe (Either e a))
-> Producer ByteString m r
-> Producer a m (Either (e, Producer ByteString m r) r)
I.consecutively Parser ByteString m (Maybe (Either DecodingError a))
forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
Parser ByteString m (Maybe (Either DecodingError a))
decode Producer ByteString m r
p0))
where
_encode :: Proxy x' x () a m (Either (a, Proxy x' x () ByteString m b) b)
-> Proxy x' x () ByteString m b
_encode = \p :: Proxy x' x () a m (Either (a, Proxy x' x () ByteString m b) b)
p -> do
Either (a, Proxy x' x () ByteString m b) b
er <- Proxy x' x () a m (Either (a, Proxy x' x () ByteString m b) b)
-> (a -> Proxy x' x () ByteString m ())
-> Proxy
x' x () ByteString m (Either (a, Proxy x' x () ByteString m b) b)
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for Proxy x' x () a m (Either (a, Proxy x' x () ByteString m b) b)
p (\a :: a
a -> (Object -> Proxy x' x () ByteString m ())
-> (Array -> Proxy x' x () ByteString m ())
-> Either Object Array
-> Proxy x' x () ByteString m ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Object -> Proxy x' x () ByteString m ()
forall (m :: * -> *).
Monad m =>
Object -> Producer' ByteString m ()
encodeObject Array -> Proxy x' x () ByteString m ()
forall (m :: * -> *). Monad m => Array -> Producer' ByteString m ()
encodeArray (Value -> Either Object Array
f (a -> Value
forall a. ToJSON a => a -> Value
Ae.toJSON a
a)))
case Either (a, Proxy x' x () ByteString m b) b
er of
Left (_, p' :: Proxy x' x () ByteString m b
p') -> Proxy x' x () ByteString m b
p'
Right r :: b
r -> b -> Proxy x' x () ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
{-# INLINABLE decoded #-}
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 :: (Value -> Either Object Array)
-> Lens'
(Producer ByteString m r)
(Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r))
decodedL f :: Value -> Either Object Array
f k :: Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r)
-> f (Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r))
k p0 :: Producer ByteString m r
p0 = (Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r)
-> Producer ByteString m r)
-> f (Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r))
-> f (Producer ByteString m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r)
-> Producer ByteString m r
forall x' x a a b.
Proxy x' x () (a, a) m (Either (a, Proxy x' x () ByteString m b) b)
-> Proxy x' x () ByteString m b
_encode (Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r)
-> f (Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r))
k (Parser ByteString m (Maybe (Either DecodingError (Int, a)))
-> Producer ByteString m r
-> Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r)
forall (m :: * -> *) e a r.
Monad m =>
Parser ByteString m (Maybe (Either e a))
-> Producer ByteString m r
-> Producer a m (Either (e, Producer ByteString m r) r)
I.consecutively Parser ByteString m (Maybe (Either DecodingError (Int, a)))
forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
Parser ByteString m (Maybe (Either DecodingError a))
decode Producer ByteString m r
p0))
where
_encode :: Proxy x' x () (a, a) m (Either (a, Proxy x' x () ByteString m b) b)
-> Proxy x' x () ByteString m b
_encode = \p :: Proxy x' x () (a, a) m (Either (a, Proxy x' x () ByteString m b) b)
p -> do
Either (a, Proxy x' x () ByteString m b) b
er <- Proxy x' x () (a, a) m (Either (a, Proxy x' x () ByteString m b) b)
-> ((a, a) -> Proxy x' x () ByteString m ())
-> Proxy
x' x () ByteString m (Either (a, Proxy x' x () ByteString m b) b)
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for Proxy x' x () (a, a) m (Either (a, Proxy x' x () ByteString m b) b)
p (\(_, a :: a
a) -> (Object -> Proxy x' x () ByteString m ())
-> (Array -> Proxy x' x () ByteString m ())
-> Either Object Array
-> Proxy x' x () ByteString m ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Object -> Proxy x' x () ByteString m ()
forall (m :: * -> *).
Monad m =>
Object -> Producer' ByteString m ()
encodeObject Array -> Proxy x' x () ByteString m ()
forall (m :: * -> *). Monad m => Array -> Producer' ByteString m ()
encodeArray (Value -> Either Object Array
f (a -> Value
forall a. ToJSON a => a -> Value
Ae.toJSON a
a)))
case Either (a, Proxy x' x () ByteString m b) b
er of
Left (_, p' :: Proxy x' x () ByteString m b
p') -> Proxy x' x () ByteString m b
p'
Right r :: b
r -> b -> Proxy x' x () ByteString m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
{-# INLINABLE decodedL #-}
type Lens' s a = forall f . Functor f => (a -> f a) -> s -> f s