我无法找到将字符串转换为的函数或解决方法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
帮助!