Question

I am trying to write a module that parses xml from an api, strips out some information, and prints the result as json, but I've hit a hiccup at the printing step. If I print shows I do indeed see that the correct data is there, however, the call which should transform the records into json, encode shows, produces a segfault.

{-# LANGUAGE Arrows, NoMonomorphismRestriction, OverloadedStrings #-}
import Network.HTTP
import Text.XML.HXT.Core
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as LazyByteString

openUrl :: String -> IO String
openUrl url = getResponseBody =<< simpleHTTP (getRequest url)

composeSearch :: String -> String
composeSearch query = "http://services.tvrage.com/feeds/search.php?show=" ++ urlEncode query

searchShow :: String -> IO String
searchShow query = openUrl $ composeSearch query

data TvShow = TvShow { showName, showId :: String } deriving (Show)

getShow = deep (isElem >>> hasName "show") >>>
    proc x -> do
        name <- getText <<< getChildren <<< deep (hasName "name")  -< x
        id <- getText <<< getChildren <<< deep (hasName "showid") -< x 
        returnA -< TvShow { showName = name, showId = id }

instance ToJSON TvShow where
    toJSON (TvShow name id) = object ["name" .= name, "id" .= id]

main :: IO ()
main = do
    results <- searchShow "Always Sunny" 
    shows <- runX (readString [ withValidate  no ] results >>> getShow)
    putStrLn (LazyByteString.unpack (encode shows))

Edit: As user1891025 has pointed out, This may be an issue with my machine or package versions.

  • System: Ubuntu 11.04 32bit
  • Aeson v0.6.1.0
  • GHC v7.2.1

This question is getting increasing specific, so I will take it to irc, but if anyone has any assistance please let me know!

Was it helpful?

Solution

Aeson's encode function calls encodeUtf8 from the Data.Text package. That function uses unsafeDupablePerformIO and other unsafe functions to poke around in memory. That's possibly where the segfault happens.

See the code here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top