Well, thatβs what I understood.
{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-} import qualified Data.ByteString.Lazy.Char8 as L import Happstack.Server import Happstack.Server.Types import Control.Monad.IO.Class (liftIO) import Data.Data (Data, Typeable) -- easiest to serialize/deserialize objects data Unit = Unit { x :: Int, y :: Int } deriving (Show, Eq, Data, Typeable) -- put this function in a library somewhere getBody :: ServerPart L.ByteString getBody = do req <- askRq body <- liftIO $ takeRequestBody req case body of Just rqbody -> return . unBody $ rqbody Nothing -> return "" myRoute :: ServerPart Response myRoute = do body <- getBody -- it a ByteString let unit = fromJust $ A.decode body :: Unit -- how to parse json ok $ toResponse $ A.encode unit -- how to send json back.
source share