Haskell aeson解码需要类型签名?
我正在编写一个小程序来解析来自TCP连接的JSON,但我遇到了一个错误:Haskell aeson解码需要类型签名?,haskell,aeson,Haskell,Aeson,我正在编写一个小程序来解析来自TCP连接的JSON,但我遇到了一个错误: Main.hs:43:22: No instance for (FromJSON t0) arising from a use of `decode' The type variable `t0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there is a
Main.hs:43:22:
No instance for (FromJSON t0) arising from a use of `decode'
The type variable `t0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there is a potential instance available:
instance FromJSON LogIn -- Defined at Main.hs:21:10
In the expression: decode lazyLine
In a pattern binding: Just login = decode lazyLine
In the expression:
do { line <- BS.hGetLine handle;
let lazyLine = BL.fromChunks ...;
let Just login = decode lazyLine;
hPutStrLn handle ".";
.... }
如果您想让Aeson为您生成实例,请确保在编译中启用扩展DeriveGenerics 您需要向该行添加注释 改变
let Just login = decode lazyLine
到
否则,您可以手动编写实例
您需要为FromJSON登录
提供一个实现了parseJSON
的typeclass实例
比如:
{-#语言重载字符串}
实例FromJSON坐标,其中
parseJSON(objectob)=登录
ob.:“用户名”
ob.:“密码”
parseJSON=mzero
如果您想让Aeson为您生成实例,请确保在编译中打开扩展DeriveGenerics
您需要向该行添加注释
改变
let Just login = decode lazyLine
到
否则,您可以手动编写实例
您需要为FromJSON登录
提供一个实现了parseJSON
的typeclass实例
比如:
{-#语言重载字符串}
实例FromJSON坐标,其中
parseJSON(objectob)=登录
ob.:“用户名”
ob.:“密码”
parseJSON=mzero
这将编译:
commandProcessor :: Handle -> IO ()
commandProcessor handle = do
line <- BS.hGetLine handle
let lazyLine = BL.fromChunks [line]
let Just login = decode lazyLine :: Maybe LogIn
hPutStrLn handle "." --(show login)
commandProcessor handle
这将汇编:
commandProcessor :: Handle -> IO ()
commandProcessor handle = do
line <- BS.hGetLine handle
let lazyLine = BL.fromChunks [line]
let Just login = decode lazyLine :: Maybe LogIn
hPutStrLn handle "." --(show login)
commandProcessor handle
我正试图让埃森为我做这项工作,如-见“结构化”。我认为这是一个简单的单子问题,因为当我看到一些东西时,警告都变成了
无法将预期类型“IO BL.ByteString”与实际类型“BL.ByteString”
匹配。我正试图让埃森为我做这项工作,如-见“结构化”。我认为这是一个简单的monad问题,因为当我看到一些东西时,警告都变成了关于无法将预期类型“IO BL.ByteString”与实际类型“BL.ByteString”匹配“
。谢谢,这就解决了问题。出于好奇,懒散是什么类型的?将其声明为BL.ByteString
或IO BL.ByteString
都会失败并出现错误。lazyline
是一个懒惰的ByteString。它的实际类型是Data.ByteString.Internal.Lazy
。值构造函数是隐藏的,因此需要使用类似于fromChunks
的函数来创建lazy bytestring类型的值。要了解更多详细信息,只需在ghci
中的decode
功能上运行:t
。感谢您的帮助,非常感谢。谢谢,这可以解决问题。出于好奇,懒散是什么类型的?将其声明为BL.ByteString
或IO BL.ByteString
都会失败并出现错误。lazyline
是一个懒惰的ByteString。它的实际类型是Data.ByteString.Internal.Lazy
。值构造函数是隐藏的,因此需要使用类似于fromChunks
的函数来创建lazy bytestring类型的值。要了解更多详情,只需在ghci
中的decode
功能上运行:t
,谢谢您的帮助,非常感谢。
commandProcessor :: Handle -> IO ()
commandProcessor handle = do
line <- BS.hGetLine handle
let lazyLine = BL.fromChunks [line]
let Just login = decode lazyLine :: Maybe LogIn
hPutStrLn handle "." --(show login)
commandProcessor handle
let Just login = decode lazyLine :: Maybe LogIn