Add encoder

This commit is contained in:
sigma-andex 2022-06-05 20:46:21 +01:00
parent 4c6d57ed1d
commit 183561d251

View File

@ -1,16 +1,20 @@
module HTTPurple.Json
( JsonDecoder(..)
, JsonEncoder(..)
, fromJson
, fromJsonE
, jsonHeader
, jsonHeaders
) where
, toJson
)
where
import Prelude
import Control.Monad.Cont (ContT(..))
import Data.Either (Either, either)
import Data.Newtype (class Newtype)
import Data.Function as Function
import Data.Newtype (class Newtype, un)
import Data.Tuple (Tuple(..))
import Effect.Aff.Class (class MonadAff)
import HTTPurple.Body (RequestBody, toString)
@ -21,6 +25,10 @@ newtype JsonDecoder err json = JsonDecoder (String -> Either err json)
instance Newtype (JsonDecoder err json) (String -> Either err json)
newtype JsonEncoder json = JsonEncoder (json -> String)
instance Newtype (JsonEncoder json) (json -> String)
jsonHeader :: Tuple String String
jsonHeader = Tuple "Content-Type" "application/json"
@ -64,3 +72,6 @@ fromJsonE driver errorHandler body = ContT $ (fromJsonContinuation driver errorH
fromJson :: forall (err :: Type) (json :: Type) (m :: Type -> Type). MonadAff m => JsonDecoder err json -> RequestBody -> ContT Response m json
fromJson driver = fromJsonE driver defaultErrorHandler
-- | Serialise a type to json using the given driver.
toJson :: forall (json :: Type). JsonEncoder json -> json -> String
toJson = un JsonEncoder >>> Function.apply