我無法找到一個函數或變通方法來轉換字符串到Data.ByteString.Lazy.Internal.ByteString
哈斯克爾埃宋JSON圖書館字節字符串問題
一個在埃宋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'來檢查。 – bennofs
列表中出現了兩個字節bytestring-0.10.0.2和bytestring-.10.4.0 - 這是否會導致問題?我知道當我玩弄上面的代碼時,我有一個GHCI鏈接錯誤。 – MathanMV
是的,我必須取消註冊bytestring-10.4.0才能使其正常工作。 – MathanMV