module Control.Proxy.Aeson.Unsafe
(
encode
, encodeD
, decode
, decodeD
, parseValue
, parseValueD
, I.DecodingError(..)
) where
import Control.Monad (unless)
import qualified Control.Proxy as P
import qualified Control.Proxy.Aeson.Internal as I
import qualified Control.Proxy.Attoparsec as PA
import qualified Control.Proxy.Trans.Either as P
import qualified Control.Proxy.Trans.State as P
import qualified Data.Aeson as Ae
import qualified Data.Aeson.Parser as Ae (value')
import qualified Data.ByteString.Char8 as B
encode
:: (P.Proxy p, Monad m, Ae.ToJSON a) => a -> p x' x () B.ByteString m ()
encode = I.fromLazy . Ae.encode
encodeD
:: (P.Proxy p, Monad m, Ae.ToJSON a) => () -> P.Pipe p a B.ByteString m r
encodeD = P.pull P./>/ encode
decode
:: (Monad m, P.Proxy p, Ae.FromJSON r)
=> P.EitherP I.DecodingError (P.StateP [B.ByteString] p)
() (Maybe B.ByteString) y' y m r
decode = do
ev <- P.liftP . P.runEitherP $ PA.parse Ae.value'
case ev of
Left e -> P.throw (I.ParserError e)
Right v ->
case Ae.fromJSON v of
Ae.Error e -> P.throw (I.ValueError e)
Ae.Success r -> return r
decodeD
:: (Monad m, P.Proxy p, Ae.FromJSON b)
=> ()
-> P.Pipe (P.EitherP I.DecodingError (P.StateP [B.ByteString] p))
(Maybe B.ByteString) b m ()
decodeD = \() -> loop where
loop = do
eof <- P.liftP $ I.skipSpace >> PA.isEndOfParserInput
unless eof $ decode >>= P.respond >> loop
parseValue
:: (Monad m, P.Proxy p)
=> P.EitherP PA.ParsingError (P.StateP [B.ByteString] p)
() (Maybe B.ByteString) y' y m Ae.Value
parseValue = PA.parse Ae.value'
parseValueD
:: (Monad m, P.Proxy p)
=> ()
-> P.Pipe (P.EitherP PA.ParsingError (P.StateP [B.ByteString] p))
(Maybe B.ByteString) Ae.Value m ()
parseValueD = \() -> loop where
loop = do
eof <- P.liftP $ I.skipSpace >> PA.isEndOfParserInput
unless eof $ parseValue >>= P.respond >> loop