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