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 8832551
In Process

The Archive Base Latest Questions

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

I’m working on the I/O aspect of my json server, and there’s a method

  • 0

I’m working on the I/O aspect of my json server, and there’s a method I just can’t get right.
First, I’ll give the error, then the code and datatypes involved and some commentary about the problem afterwards.

("X-Response-Body-Start","<!DOCTYPE html>\n<html><head><title>Invalid Arguments</title></head><body><h1>Invalid Arguments</h1><ul><li>when expecting a unit constructor (U1), encountered String instead</li></ul></body></html>")

Expecting unit contructor?

Okay here’s some relevant code. Let’s see if we can see where I go wrong
from Datatypes.hs

data JobID = JobID Project Int deriving Generic

data Project = BNAP deriving (Show,Generic) -- one day to be an ADT

instance ToJSON Project where
   toJSON = toJSON . show

instance FromJSON Project

instance FromJSON JobID
instance ToJSON JobID

The test code

testReadR :: IO Value
testReadR = do
   req <- parseUrl readURI
   manager <- newManager def
   pBody <- runResourceT $ do
               reqBody <- readObject
               liftIO $ print reqBody
               Response _ _ _ body <- http (buildReq req reqBody) manager
               pBody <- body $$+- sinkParser json
               return pBody  -- (return wraps it up)
   closeManager manager
   return pBody

buildReq :: forall a (m :: * -> *) (t :: * -> *).
            ToJSON a =>
            Request t -> a -> Request m
buildReq req reqBody =
   let reqBodyBS = Data.Aeson.encode reqBody
       rHeaders = [(hContentType,pack "application/json")]
   in  req {method = fromString "POST"
           , requestBody = RequestBodyLBS reqBodyBS
           ,requestHeaders=rHeaders
           }
readObject :: ResourceT IO Value
readObject = do -- I took a bunch out because I thought simplifiying would help me
                -- solve this
   return $ Data.Aeson.toJSON $ JobID BNAP 306

The Handler

postReadR :: Handler RepJson
postReadR = do
   conf <- parseJsonBody_ :: Handler JobID        
   liftIO $ print conf
   testJ <- jsonToRepJson $ toJSON $ JobID BNAP 305
   jValue <- jsonToRepJson conf -- to be replaced with 
                                -- Either ErrorReport Response
                                -- (or something like that)
   return jValue

when I change the line to
conf <- parseJsonBody_ :: Handler Value

print conf yields
Array (fromList [String "BNAP",Number 306])

So it seems the problem lies with String “BNAP” but I don’t know why. Any ideas on how I can suss this out? Is there an obvious answer I’m not seeing?

Update : I have a new error. I’m sure I borked the FromJSON instance.

test: ResponseTimeout

instance FromJSON Project where
   parseJSON (String p) = parseJSON $ toJSON p
   parseJSON _ = mzero

The challenge here was that Project is a unary type. None of the examples I studied seemed to address that. But I know p is a Text, and toJSON can make a Value out of that, and parseJSON makes a Parser out of the Value. So it’s all good right? Well, I still get the above error which is not informative at all. Any ideas?

  • 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-14T08:33:32+00:00Added an answer on June 14, 2026 at 8:33 am
    instance ToJSON Project where
       toJSON = toJSON . show
    
    instance FromJSON Project
    

    The instances don’t work together. The generic FromJSON instance expects the generic unit constructor U1, but the ToJSON instance produces a String "BNAP".

    If you have a hand-written ToJSON instance, you also need a hand-written FromJSON instance.


    instance FromJSON Project where
       parseJSON (String p) = parseJSON $ toJSON p
       parseJSON _ = mzero
    

    p is a Text, and we have

    instance ToJSON Text where
        toJSON = String
    

    so the above instance for Project loops, since it expands parseJSON (String p) = parseJSON (String p).

    For the type as it stands now,

    instance FromJSON Project where
        parseJSON (String "BNAP") = return BNAP
        parseJSON _               = mzero
    

    should work if you have OverloadedStrings enabled, if not,

    instance FromJSON Project where
        parseJSON (String p)
            | p == pack "BNAP" = return BNAP
        parseJSON _            = mzero
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string

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.