haskell - is this a bug of 32bit GHC on MAC -
to make long stroy short, code parse json file using aeson
here 2 pieces of code:
a.hs
{-# language overloadedstrings #-} import data.aeson import qualified data.bytestring.lazy.char8 c import control.monad import control.applicative data auctioninfo = auctioninfo { realm :: realm , alliance :: auctions , horde :: auctions , neutral :: auctions } deriving (show ) instance fromjson auctioninfo parsejson (object o) = r <- o .: "realm" >>= parsejson <- o .: "alliance" >>= parsejson h <- o .: "horde" >>= parsejson n <- o .: "neutral" >>= parsejson return $ auctioninfo r h n parsejson _ = mzero data realm = realm { name2 :: string , slug:: string} deriving (show ) instance fromjson realm parsejson (object o) = realm <$> o .: "name" <*> o .: "slug" parsejson _ = mzero data auctions = auctions {auctions :: [auc]} deriving (show) instance fromjson auctions parsejson (object o ) = auctions <$> o.: "auctions" parsejson _ = mzero data auc = auc { auc :: integer, itme :: int, owner :: string, bid :: integer, buyout ::integer, quantity :: int, timeleft :: string, rand :: integer, seed :: integer } deriving (show ) instance fromjson auc parsejson (object o ) = auc <$> o .: "auc" <*> o .: "item" <*> o .: "owner" <*> o .: "bid" <*> o .: "buyout" <*> o .: "quantity" <*> o .: "timeleft" <*> o .: "rand" <*> o .: "seed" parsejson _ = mzero main = au<- c.readfile "a.json" let x = decode au :: maybe auctioninfo case x of -> {-putstrln.show $ a-} putstrln .show.length.auctions.alliance $ putstrln "ok" nothing -> putstrln "fail"
and test steps:
- save code , , name a.hs (or want)
- save test data ,name a.json (do not change name)
- if have not install
aeson
,$ cabal install aseon
$ ghc a.hs -o a
$ ./a
what output "fail".
and when run command $ runghc a.hs
few times , got ok
, fail
mixed together.
i have tried code on linux , 64bit mac ghc, output ok
expected.
one of friends has tried code on 32bit mac ghc, fail
too. , told me played black magic code , changed 1 line into
let x = decode $(c.pack. c.unpack) au :: maybe auctioninfo
then output ok
. when did same black magic, output still fail
.
i want make sure bug or bug of ghc, or how can determine that.
i'm not sure if behaviour related this, absolutely shouldn't use data.bytestring.lazy.char8
since that's 8-bit ascii data , input utf-8.
try replacing import
import qualified data.bytestring.lazy bl
and use bl.readfile
read in data (of course actual name doesn't matter, bl
idiomatic shorthand lazy bytestring package).
note use data.text
handling unicode text, in case aeson
api expects binary (i.e. bytestring
) representation , handles decoding unicode internally.
edit: i've thought more, don't think problem using char8
after (although point stands not using unicode text in general) not doing conversions string
or char
(expect c.pack . c.unpack
experiment, break multi-byte characters).
Comments
Post a Comment