我正在编写一个小程序来解析来自 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 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 ".";
.... }
我不知道要添加什么签名instance FromJSON LogIn
。
代码是:
{-# LANGUAGE DeriveGeneric #-}
module Main(main) where
import Network (listenOn, withSocketsDo, accept, PortID(..), Socket)
import System (getArgs)
import System.IO (hSetBuffering, hPutStrLn, BufferMode(..), Handle)
import Control.Concurrent (forkIO)
import qualified Data.ByteString as BS
import Data.Text
import Data.Aeson
import GHC.Generics
import qualified Data.ByteString.Lazy as BL
data LogIn = LogIn
{ username :: String
, password :: String
} deriving (Show, Generic)
instance FromJSON LogIn
main :: IO ()
main = withSocketsDo $ do
sock <- listenOn $ PortNumber 3333
putStrLn $ "Listening on " ++ "3333"
sockHandler sock
sockHandler :: Socket -> IO ()
sockHandler sock = do
(handle, _, _) <- accept sock
hSetBuffering handle NoBuffering
forkIO $ commandProcessor handle
sockHandler sock
commandProcessor :: Handle -> IO ()
commandProcessor handle = do
line <- BS.hGetLine handle
let lazyLine = BL.fromChunks [line]
let Just login = decode lazyLine
hPutStrLn handle "." --(show login)
commandProcessor handle