【发布时间】:2013-12-12 20:53:27
【问题描述】:
我无法找到将 String 转换为 Data.ByteString.Lazy.Internal.ByteString
Aeson Json库中的一个函数是decode,有如下描述:
decode :: FromJSON a => bytestring-0.10.0.2:Data.ByteString.Lazy.Internal.ByteString -> Maybe a
我尝试在 Data.ByteString.Lazy.Char8 中使用 pack 函数,但它返回不同的 ByteString。有谁知道如何解决这个问题?
以下是我正在处理的示例:
import Data.Aeson
import Data.Text
import Control.Applicative
import Control.Monad (mzero)
import qualified Data.ByteString.Lazy.Internal as BLI
import qualified Data.ByteString.Lazy.Char8 as BSL
data Person = Person
{ name :: Text
, age :: Int
} deriving Show
instance FromJSON Person where
parseJSON (Object v) = Person <$>
v .: (pack "name") <*>
v .: (pack "age")
parseJSON _ = mzero
我尝试使用decode (BSL.pack "{\"name\":\"Joe\",\"age\":12}") :: Maybe Person
并收到以下错误消息:
Couldn't match expected type `bytestring-0.10.0.2:Data.ByteString.Lazy.Internal.ByteString'
with actual type `BSL.ByteString'
In the return type of a call of `BSL.pack'
In the first argument of `decode', namely
`(BSL.pack "{\"name\":\"Joe\",\"age\":12}")'
In the expression:
decode (BSL.pack "{\"name\":\"Joe\",\"age\":12}") :: Maybe Person
救命!
【问题讨论】:
-
您是否安装了多个版本的字节串?尝试运行
ghc-pkg list bytestring进行检查。 -
该列表包含两个项目 bytestring-0.10.0.2 和 bytestring-.10.4.0 - 这会导致问题吗?我知道我之前在玩上面的代码时遇到了 GHCI 链接错误。
-
是的,我必须取消注册 bytestring-10.4.0 才能让它工作。