1 {-# LANGUAGE OverloadedStrings #-}
3 module JsonRpc ( JsonRpcVersion(JsonRpcV1, JsonRpcV2)
11 import Data.ByteString.Lazy (ByteString)
12 import Control.Applicative ((<$>), (<*>), empty)
16 data JsonRpcVersion = JsonRpcV1 | JsonRpcV2
19 data JsonRpcRequest = JsonRpcRequest { jrpcVersion :: JsonRpcVersion
20 , jrpcReqMethod :: ByteString
21 , jrpcReqParams :: [ByteString]
22 , jrpcReqId :: ByteString
24 instance ToJSON JsonRpcRequest where
25 toJSON (JsonRpcRequest version method params id) =
26 let l = [ "method" .= method, "params" .= params, "id" .= id ]
29 JsonRpcV2 -> object $ ("jsonrpc" .= toJSON ("2.0" :: ByteString)):l
31 data JsonRpcNotification = JsonRpcNotification
32 { jrpcNtfVersion :: JsonRpcVersion
33 , jrpcNtfMethod :: ByteString
34 , jrpcNtfParams :: [ByteString]
36 instance ToJSON JsonRpcNotification where
37 toJSON (JsonRpcNotification version method params) =
38 let l = [ "method" .= method, "params" .= params ]
41 JsonRpcV2 -> object $ ("jsonrpc" .= toJSON ("2.0" :: ByteString)):l
43 data JsonRpcError = JsonRpcError { jrpcErrCode :: Int
44 , jrpcErrMessage :: ByteString
45 , jrpcErrData :: Maybe Value
48 data JsonRpcResponse = JsonRpcResponse { jrpcRspResult :: Maybe Value
49 , jrpcRspError :: JsonRpcError
50 , jrpcRspId :: ByteString
53 parseJsonRpc :: ByteString -> Either JsonRpcError JsonRpcResponse
54 parseJsonRpc _ = Left $ JsonRpcError (-1) "someerror" Nothing