{-# LANGUAGE RankNTypes #-}
module Pipes.Aeson
(
encodeArray
, encodeObject
, decode
, decoded
, loop
, decodeL
, decodedL
, loopL
, I.DecodingError(..)
) where
import qualified Data.Aeson as Ae
import qualified Data.Aeson.Parser 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 -> Proxy x' x () B.ByteString m ()
encodeObject :: forall (m :: * -> *) x' x.
Monad m =>
Object -> Proxy x' x () ByteString m ()
encodeObject = Object -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x.
(Monad m, ToJSON a) =>
a -> Proxy x' x () 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 -> Proxy x' x () B.ByteString m ()
encodeArray :: forall (m :: * -> *) x' x.
Monad m =>
Array -> Proxy x' x () ByteString m ()
encodeArray = Array -> Proxy x' x () ByteString m ()
forall (m :: * -> *) a x' x.
(Monad m, ToJSON a) =>
a -> Proxy x' x () 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 :: forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
Parser ByteString m (Maybe (Either DecodingError a))
decode = (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 a b.
(a -> b)
-> StateT (Producer ByteString m x) m a
-> StateT (Producer ByteString m x) m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Either DecodingError (Int, a) -> Either DecodingError a)
-> Maybe (Either DecodingError (Int, a))
-> Maybe (Either DecodingError a)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((Int, a) -> a)
-> Either DecodingError (Int, a) -> Either DecodingError a
forall a b.
(a -> b) -> Either DecodingError a -> Either DecodingError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, a) -> a
forall a b. (a, b) -> b
snd)) StateT
(Producer ByteString m x) m (Maybe (Either DecodingError (Int, a)))
Parser ByteString 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 :: forall (m :: * -> *) a.
(Monad m, FromJSON a) =>
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 :: forall (m :: * -> *) a r.
(Monad m, FromJSON a, ToJSON a) =>
(Value -> Either Object Array)
-> Lens'
(Producer ByteString m r)
(Producer a m (Either (DecodingError, Producer ByteString m r) r))
decoded Value -> Either Object Array
f Producer a m (Either (DecodingError, Producer ByteString m r) r)
-> f (Producer
a m (Either (DecodingError, Producer ByteString m r) r))
k 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 a b. (a -> b) -> f a -> f b
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 StateT (Producer ByteString m x) m (Maybe (Either DecodingError a))
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 = \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 -> (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 :: * -> *) x' x.
Monad m =>
Object -> Proxy x' x () ByteString m ()
encodeObject Array -> Proxy x' x () ByteString m ()
forall (m :: * -> *) x' x.
Monad m =>
Array -> Proxy x' x () 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 (a
_, Proxy x' x () ByteString m b
p') -> Proxy x' x () ByteString m b
p'
Right b
r -> b -> Proxy x' x () ByteString m b
forall a. a -> Proxy x' x () ByteString m a
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 :: forall (m :: * -> *) a r.
(Monad m, FromJSON a, ToJSON a) =>
(Value -> Either Object Array)
-> Lens'
(Producer ByteString m r)
(Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r))
decodedL Value -> Either Object Array
f Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r)
-> f (Producer
(Int, a) m (Either (DecodingError, Producer ByteString m r) r))
k 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 a b. (a -> b) -> f a -> f b
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 StateT
(Producer ByteString m x) m (Maybe (Either DecodingError (Int, a)))
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 = \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 :: * -> *) x' x.
Monad m =>
Object -> Proxy x' x () ByteString m ()
encodeObject Array -> Proxy x' x () ByteString m ()
forall (m :: * -> *) x' x.
Monad m =>
Array -> Proxy x' x () 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 (a
_, Proxy x' x () ByteString m b
p') -> Proxy x' x () ByteString m b
p'
Right b
r -> b -> Proxy x' x () ByteString m b
forall a. a -> Proxy x' x () ByteString m a
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
{-# INLINABLE decodedL #-}
loop
:: (Monad m, Ae.FromJSON a)
=> (Pipes.Producer B.ByteString m r -> Pipes.Producer B.ByteString m r)
-> Pipes.Producer B.ByteString m r
-> Pipes.Producer' (Either I.DecodingError a) m r
{-# INLINABLE loop #-}
loop :: forall (m :: * -> *) a r.
(Monad m, FromJSON a) =>
(Producer ByteString m r -> Producer ByteString m r)
-> Producer ByteString m r
-> Producer' (Either DecodingError a) m r
loop Producer ByteString m r -> Producer ByteString m r
fp Producer ByteString m r
p0 = Proxy x' x () (Either DecodingError (Int, a)) m r
-> (Either DecodingError (Int, a)
-> Proxy x' x () (Either DecodingError a) m ())
-> Proxy x' x () (Either DecodingError a) m r
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'
Pipes.for ((Producer ByteString m r -> Producer ByteString m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
forall (m :: * -> *) a r x' x.
(Monad m, FromJSON a) =>
(Producer ByteString m r -> Producer ByteString m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
loopL Producer ByteString m r -> Producer ByteString m r
fp Producer ByteString m r
p0) (Either DecodingError a
-> Proxy x' x () (Either DecodingError a) m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
Pipes.yield (Either DecodingError a
-> Proxy x' x () (Either DecodingError a) m ())
-> (Either DecodingError (Int, a) -> Either DecodingError a)
-> Either DecodingError (Int, a)
-> Proxy x' x () (Either DecodingError a) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, a) -> a)
-> Either DecodingError (Int, a) -> Either DecodingError a
forall a b.
(a -> b) -> Either DecodingError a -> Either DecodingError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, a) -> a
forall a b. (a, b) -> b
snd)
loopL
:: (Monad m, Ae.FromJSON a)
=> (Pipes.Producer B.ByteString m r -> Pipes.Producer B.ByteString m r)
-> Pipes.Producer B.ByteString m r
-> Pipes.Proxy x' x () (Either I.DecodingError (Int, a)) m r
{-# INLINABLE loopL #-}
loopL :: forall (m :: * -> *) a r x' x.
(Monad m, FromJSON a) =>
(Producer ByteString m r -> Producer ByteString m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
loopL = Parser ByteString Value
-> (Producer ByteString m r -> Producer ByteString m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
forall (m :: * -> *) a r x' x.
(Monad m, FromJSON a) =>
Parser ByteString Value
-> (Producer ByteString m r -> Producer ByteString m r)
-> Producer ByteString m r
-> Proxy x' x () (Either DecodingError (Int, a)) m r
I.loopL Parser ByteString Value
Ae.json'
type Lens' s a = forall f . Functor f => (a -> f a) -> s -> f s