1

My understanding is that if I use the json parser with parse on json data, I would get json types back. Unless,of course, the data is mal-formed. But then I would expect a failed parse.

This is all done in ghci.

>test <- (parseUrl "https://pkg.cudaops.com/cgi-bin/qaLinkEditor.cgi?json=1") :: (IO (Request IO))
>nManager <- newManager
>catch <- httpLbsRedirect test nManager
>let catchChunks = toChunks $ responseBody catch
>let flatChunks = Data.ByteString.concat catchChunks
>let parsed = parse json flatChunks
>:t parsed
>parsed :: Data.Attoparsec.Result Value

The result is rather large, Here's part of the beginning of parsed that I wrote out to file

Done "\n" Object (fromList [("name",String "versions"),("products",Object (fromList [("BCC Admin",Object (fromList [("available",Array (fromList [String ..."

How am I getting fromList? Where it says"Object (fromList [("available",Array... ", I expected "Object(Array ["available",Array ..." Am I misusing parse? Could I just be misunderstanding how parse works, it did what it was supposed to do and my expectations were wrong?

2 Answers2

3

That's just how arrays print. The convention is that Show instances should give valid Haskell code that recreates the value.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
2

I've not used Attoparsec before, but I assume it's just the Show implementation that's giving you the fromList.

Data.Set has this behavior as well:

Prelude> import Data.Set
Prelude Data.Set> :t fromList
fromList :: Ord a => [a] -> Set a
Prelude Data.Set> let s = fromList [1, 2, 3]
Prelude Data.Set> :t s
s :: Set Integer       -- The value *is* a set, as the type tells us
Prelude Data.Set> s
fromList [1,2,3]       -- This is just the string repr of the set

If you asked ghci for the type of fromList, I assume you would see the type you were looking for as the result. Just as I see Set a as the result type of fromList in this scenario.

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66