Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:59:33+00:00 2026-06-14T00:59:33+00:00

Continuing my exploration of conduit and aeson, how would I go about using my

  • 0

Continuing my exploration of conduit and aeson, how would I go about using my own data type in stead of Value in this (slightly modified) code snippet from the Yesod book.

{-# LANGUAGE OverloadedStrings, TemplateHaskell #-}
import Network.Wai (Response, responseLBS, Application, requestBody)
import Network.HTTP.Types (status200, status400)
import Network.Wai.Handler.Warp (run)
import Data.Aeson.Parser (json)
import Data.Conduit.Attoparsec (sinkParser)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (Value(..), encode, object, (.=))
import Control.Exception (SomeException)
import Data.ByteString (ByteString)
import Data.Conduit (ResourceT, ($$))
import Control.Exception.Lifted (handle)

import qualified Data.HashMap.Strict as M
import Data.Aeson.TH (deriveJSON)

-- I ADDED THIS

data JSONRequest = JSONRequest {
    command :: ByteString,
    params :: M.HashMap ByteString ByteString
}

deriveJSON id ''JSONRequest

-- END OF WHAT I ADDED

main :: IO ()
main = run 3000 app

app :: Application
app req = handle invalidJson $ do
    value <- requestBody req $$ sinkParser json
    newValue <- liftIO $ dispatch value
    return $ responseLBS
        status200
        [("Content-Type", "application/json")]
        $ encode newValue

invalidJson :: SomeException -> ResourceT IO Response
invalidJson ex = return $ responseLBS
    status400
    [("Content-Type", "application/json")]
    $ encode $ object
        [ ("message" .= show ex)
        ]

-- Application-specific logic would go here.
dispatch :: Value -> IO Value
dispatch = return

Basically, I want to change the type of dispatch to JSONRequest -> IO JSONRequest. How do I tell the parser to use my own derived instance of fromJSON?

I tried just adding a type declaration, praying for polymorphic return type on json, but I realised it is strictly for Value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:59:34+00:00Added an answer on June 14, 2026 at 12:59 am

    Just looking at the types, don’t you just need to fmap your fromJSON over the result coming from json? With a suitable signature for dispatch we just need:

    -- import Data.Aeson
    app :: Application
    app req = handle invalidJson $ do
          result <- requestBody req $$ sinkParser (fmap fromJSON json)
          next_result <- liftIO $ dispatch result
          return $ responseLBS status200 [("Content-Type", "application/json")] 
                 $ encode next_result   
    
    dispatch :: Result JSONRequest -> IO JSONRequest
    dispatch (Error str) = undefined
    dispatch (Success jsonreq) = return jsonreq
    

    But maybe it’s a little clearer written thus:

    -- import Data.Aeson
    -- import qualified Data.Attoparsec as Atto
    toRequest ::    Value -> Result JSONRequest
    toRequest = fromJSON   -- specialized now to your fromJSON
    
    jsonRequestParser :: Atto.Parser (Result JSONRequest)
    jsonRequestParser = fmap toRequest json 
    
    app :: Application
    app req = handle invalidJson $ do
          result <- requestBody req $$ sinkParser jsonRequestParser
          next_result <- liftIO $ dispatch result
          return $ responseLBS status200 [("Content-Type", "application/json")]
                 $ encode next_result   
    
    dispatch :: Result JSONRequest -> IO JSONRequest
    dispatch (Error str) = undefined
    dispatch (Success jsonreq) = return jsonreq
    

    I left the parser returning a Result JSONRequest so dispatch is handling Error cases too, which might mean you need your exception handling somehow?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing my quest to perform a dropbox query I am using http-conduit to try
Continuing the discussion about the minimum footprint needed to connect to an Oracle DB
Continuing from this question : When I am trying to do fopen on Windows,
I've seen a couple of questions about this particular error message, but couldn't find
I would like to mention before continuing that I have looked at other questions
Continuing adopting my code to work with IE... I have a hidden div containing
Continuing on previous questions ( here , and here ), I implemented a basic
Continuing on my attempt to create a DateTime class , I am trying to
Continuing my quest of learning Java by doing a simple game, i stumbled upon
I am facing a continuing problem distinguishing delegation, composition and aggregation from each other,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.