I tried to run code from http://gregorycollins.net/posts/2011/10/01/cufp2011/index.html#(43) in GHCi, but got “Floating point exception, and GHCi quits.
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Aeson
import Data.Attoparsec (parseOnly)
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy.Char8 as L
------------------------------------------------------------------------------
example1 :: ByteString -> Either String Coord
example1 bs = parseOnly json bs >>= convert
where
convert value = case fromJSON value of
(Error e) -> Left e
(Success a) -> Right a
example2 :: Coord -> ByteString
example2 c = S.concat $ L.toChunks $ encode c
------------------------------------------------------------------------------
data Coord = Coord { _x :: Double, _y :: Double }
deriving (Show, Eq)
instance ToJSON Coord where
toJSON (Coord x y) = object ["x" .= x, "y" .= y]
instance FromJSON Coord where
parseJSON (Object v) = Coord <$>
v .: "x" <*>
v .: "y"
-- A non-Object value is of the wrong type, so use mzero to fail.
parseJSON _ = empty
λ> :l JsonExample.hs
[1 of 1] Compiling Main ( JsonExample.hs, interpreted )
Ok, modules loaded: Main.
λ> example2 $ Coord 1 1
"Floating point exception
The occuring FPE is a GHC bug, see http://hackage.haskell.org/trac/ghc/ticket/5386 for more details.
You can try to use the http://hackage.haskell.org/package/aeson-native package instead (it’s API compatible to
aeson), which avoids using the C++ library that causes this issue (by usingblaze-textual-native), or you can try recompilingblaze-textual(and then also recompileaesonso that it picks up the recompiledblaze-textual) in native mode by(see https://github.com/mailrank/blaze-textual/blob/master/README.markdown for more details)